Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/configs/groovy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ outputDir: samples/client/petstore/groovy
inputSpec: modules/openapi-generator/src/test/resources/3_0/groovy/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/Groovy
additionalProperties:
enumPropertyNaming: "original"
hideGenerationTimestamp: "true"
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package {{invokerPackage}}

{{#withXml}}
import java.io.StringReader
import {{javaxPackage}}.xml.bind.JAXB
{{/withXml}}
import groovy.json.JsonBuilder
import groovy.json.JsonGenerator
import groovyx.net.http.ChainedHttpConfig
import groovyx.net.http.ContentTypes
import groovyx.net.http.NativeHandlers
import groovyx.net.http.FromServer
import groovyx.net.http.ToServer

import static groovyx.net.http.HttpBuilder.configure
Expand All @@ -18,36 +23,59 @@ class ApiUtils {
}
.build()

void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType, method, container, type) {
void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType, method, container, type) {
def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
println "url=$url uriPath=$uriPath"
def http = configure {
request.uri = url
request.uri.path = uriPath
request.encoder(ContentTypes.JSON, { final ChainedHttpConfig config, final ToServer ts ->
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
return;
return
}

final Object body = NativeHandlers.Encoders.checkNull(request.actualBody());
final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
final String json = ((body instanceof String || body instanceof GString)
? body.toString()
: new JsonBuilder(body, jsonGenerator).toString());
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()));
: new JsonBuilder(body, jsonGenerator).toString())
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()))
})
{{#withXml}}
request.encoder(ContentTypes.XML, { final ChainedHttpConfig config, final ToServer ts ->
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
return
}

final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
String xml
if (body instanceof String || body instanceof GString) {
xml = body.toString()
} else {
StringWriter writer = new StringWriter()
JAXB.marshal(body, writer)
xml = writer.toString()
}
ts.toServer(NativeHandlers.Encoders.stringToStream(xml, request.actualCharset()))
}){{/withXml}}
}
.invokeMethod(String.valueOf(method).toLowerCase()) {
request.uri.query = queryParams
request.headers = headerParams
if (bodyParams != null) {
request.body = bodyParams
}
request.accept = accept
request.contentType = contentType

response.success { resp, json ->
{{#withXml}}
response.parser(ContentTypes.XML) { final ChainedHttpConfig cfg, final FromServer fs ->
fs.inputStream.text
}
{{/withXml}}
response.success { resp, body ->
if (type != null) {
onSuccess(parse(json, container, type))
onSuccess(parse(resp, body, container, type))
}
}
response.failure { resp ->
Expand All @@ -68,12 +96,24 @@ class ApiUtils {
[basePath-pathOnly, pathOnly+versionPath+resourcePath]
}

private def parse(object, container, clazz) {
private def parse(response, object, container, clazz) {
{{#withXml}}
if (response.getContentType().toLowerCase().contains("xml")) {
return JAXB.unmarshal(new StringReader(object), clazz)
}
{{/withXml}}
if (container == "array") {
return object.collect {parse(it, "", clazz)}
} else {
return object.collect { parse(response, it, "", clazz) }
} else {
return clazz.newInstance(object)
}
}

private def selectHeaderAccept(accepts) {
def jsonMime = 'application/json'
if (accepts.find { it.toLowerCase().startsWith(jsonMime) }) {
return [jsonMime]
}
return accepts
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class {{classname}} {
def queryParams = [:]
def headerParams = [:]
def bodyParams
def accept
def contentType

{{#allParams}}
Expand Down Expand Up @@ -71,7 +72,9 @@ class {{classname}} {
{{/formParams}}
{{/hasFormParams}}

apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
accept = apiUtils.selectHeaderAccept([{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}])

apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
"{{httpMethod}}", "{{returnContainer}}",
{{#returnBaseType}}{{{.}}}.class {{/returnBaseType}}{{^returnBaseType}}null {{/returnBaseType}})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'com.github.johnrengelman.shadow'

group = '{{groupId}}'
version = '{{artifactVersion}}'
Expand All @@ -18,6 +19,7 @@ buildscript {
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '4.24.20')
classpath('com.github.johnrengelman.shadow:com.github.johnrengelman.shadow.gradle.plugin:4.0.4')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import groovy.transform.Canonical
{{#imports}}
import {{import}};
{{/imports}}
{{#withXml}}
import {{javaxPackage}}.xml.bind.annotation.*;
{{/withXml}}

{{#models}}
{{#model}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{{#withXml}}
@XmlAccessorType(XmlAccessType.NONE)
{{/withXml}}
@Canonical
class {{classname}} {
{{#vars}}
Expand All @@ -16,6 +19,12 @@ class {{classname}} {

{{/isEnum}}
{{#description}}/* {{{.}}} */{{/description}}
{{#withXml}}
@Xml{{#isXmlAttribute}}Attribute{{/isXmlAttribute}}{{^isXmlAttribute}}Element{{/isXmlAttribute}}(name = "{{items.xmlName}}{{^items.xmlName}}{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}{{/items.xmlName}}"{{#xmlNamespace}}, namespace = "{{.}}"{{/xmlNamespace}})
{{#isXmlWrapped}}
@XmlElementWrapper(name = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}"{{#xmlNamespace}}, namespace = "{{.}}"{{/xmlNamespace}})
{{/isXmlWrapped}}
{{/withXml}}
{{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}
{{/vars}}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{{#withXml}}
@XmlEnum
{{/withXml}}
enum {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
{{#allowableValues}}{{#enumVars}}
{{#enumDescription}}
/**
* {{.}}
*/
{{/enumDescription}}
{{#withXml}}
@XmlEnumValue({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
{{/withXml}}
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{/enumVars}}{{/allowableValues}}

Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/groovy/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'com.github.johnrengelman.shadow'

group = 'org.openapitools'
version = '1.0.0'
Expand All @@ -18,6 +19,7 @@ buildscript {
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '4.24.20')
classpath('com.github.johnrengelman.shadow:com.github.johnrengelman.shadow.gradle.plugin:4.0.4')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import groovy.json.JsonGenerator
import groovyx.net.http.ChainedHttpConfig
import groovyx.net.http.ContentTypes
import groovyx.net.http.NativeHandlers
import groovyx.net.http.FromServer
import groovyx.net.http.ToServer

import static groovyx.net.http.HttpBuilder.configure
Expand All @@ -18,36 +19,37 @@ class ApiUtils {
}
.build()

void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType, method, container, type) {
void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType, method, container, type) {
def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
println "url=$url uriPath=$uriPath"
def http = configure {
request.uri = url
request.uri.path = uriPath
request.encoder(ContentTypes.JSON, { final ChainedHttpConfig config, final ToServer ts ->
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
return;
return
}

final Object body = NativeHandlers.Encoders.checkNull(request.actualBody());
final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
final String json = ((body instanceof String || body instanceof GString)
? body.toString()
: new JsonBuilder(body, jsonGenerator).toString());
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()));
: new JsonBuilder(body, jsonGenerator).toString())
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()))
})

}
.invokeMethod(String.valueOf(method).toLowerCase()) {
request.uri.query = queryParams
request.headers = headerParams
if (bodyParams != null) {
request.body = bodyParams
}
request.accept = accept
request.contentType = contentType

response.success { resp, json ->
response.success { resp, body ->
if (type != null) {
onSuccess(parse(json, container, type))
onSuccess(parse(resp, body, container, type))
}
}
response.failure { resp ->
Expand All @@ -68,12 +70,19 @@ class ApiUtils {
[basePath-pathOnly, pathOnly+versionPath+resourcePath]
}

private def parse(object, container, clazz) {
private def parse(response, object, container, clazz) {
if (container == "array") {
return object.collect {parse(it, "", clazz)}
} else {
return object.collect { parse(response, it, "", clazz) }
} else {
return clazz.newInstance(object)
}
}

private def selectHeaderAccept(accepts) {
def jsonMime = 'application/json'
if (accepts.find { it.toLowerCase().startsWith(jsonMime) }) {
return [jsonMime]
}
return accepts
}
}
Loading
Loading