forked from loafle/openapi-generator-original
[groovy] support enum generation in groovy client (#15619)
* support generation of enums for groovy * add custom json generator for groovy * add nestet enums in groovy templates * add indent for nested Groovy enums & fix compilation errors in Groovy ApiUtils * save auto generated samples&docs for groovy
This commit is contained in:
parent
358397fe3f
commit
5b5cb1f7e0
@ -1,16 +1,41 @@
|
|||||||
package {{invokerPackage}}
|
package {{invokerPackage}}
|
||||||
|
|
||||||
|
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.ToServer
|
||||||
|
|
||||||
import static groovyx.net.http.HttpBuilder.configure
|
import static groovyx.net.http.HttpBuilder.configure
|
||||||
import static java.net.URI.create
|
import static java.net.URI.create
|
||||||
|
|
||||||
class ApiUtils {
|
class ApiUtils {
|
||||||
|
|
||||||
|
static def jsonGenerator = new JsonGenerator.Options()
|
||||||
|
.addConverter(Enum) { Enum u, String key ->
|
||||||
|
u.toString()
|
||||||
|
}
|
||||||
|
.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, contentType, method, container, type) {
|
||||||
def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
|
def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
|
||||||
println "url=$url uriPath=$uriPath"
|
println "url=$url uriPath=$uriPath"
|
||||||
def http = configure {
|
def http = configure {
|
||||||
request.uri = url
|
request.uri = url
|
||||||
request.uri.path = uriPath
|
request.uri.path = uriPath
|
||||||
|
request.encoder(ContentTypes.JSON, { 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());
|
||||||
|
final String json = ((body instanceof String || body instanceof GString)
|
||||||
|
? body.toString()
|
||||||
|
: new JsonBuilder(body, jsonGenerator).toString());
|
||||||
|
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
.invokeMethod(String.valueOf(method).toLowerCase()) {
|
.invokeMethod(String.valueOf(method).toLowerCase()) {
|
||||||
request.uri.query = queryParams
|
request.uri.query = queryParams
|
||||||
|
@ -7,12 +7,6 @@ import {{import}};
|
|||||||
|
|
||||||
{{#models}}
|
{{#models}}
|
||||||
{{#model}}
|
{{#model}}
|
||||||
@Canonical
|
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelClass}}{{/isEnum}}
|
||||||
class {{classname}} {
|
|
||||||
{{#vars}}
|
|
||||||
{{#description}}/* {{{.}}} */{{/description}}
|
|
||||||
{{{dataType}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}
|
|
||||||
{{/vars}}
|
|
||||||
}
|
|
||||||
{{/model}}
|
{{/model}}
|
||||||
{{/models}}
|
{{/models}}
|
||||||
|
21
modules/openapi-generator/src/main/resources/Groovy/modelClass.mustache
vendored
Normal file
21
modules/openapi-generator/src/main/resources/Groovy/modelClass.mustache
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
@Canonical
|
||||||
|
class {{classname}} {
|
||||||
|
{{#vars}}
|
||||||
|
{{#isEnum}}
|
||||||
|
|
||||||
|
{{^isContainer}}
|
||||||
|
{{#lambda.indented}}
|
||||||
|
{{>modelEnum}}{{/lambda.indented}}
|
||||||
|
{{/isContainer}}
|
||||||
|
{{#isContainer}}
|
||||||
|
{{#mostInnerItems}}
|
||||||
|
{{#lambda.indented}}
|
||||||
|
{{>modelEnum}}{{/lambda.indented}}
|
||||||
|
{{/mostInnerItems}}
|
||||||
|
{{/isContainer}}
|
||||||
|
|
||||||
|
{{/isEnum}}
|
||||||
|
{{#description}}/* {{{.}}} */{{/description}}
|
||||||
|
{{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}
|
||||||
|
{{/vars}}
|
||||||
|
}
|
25
modules/openapi-generator/src/main/resources/Groovy/modelEnum.mustache
vendored
Normal file
25
modules/openapi-generator/src/main/resources/Groovy/modelEnum.mustache
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
enum {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
|
||||||
|
{{#allowableValues}}{{#enumVars}}
|
||||||
|
{{#enumDescription}}
|
||||||
|
/**
|
||||||
|
* {{.}}
|
||||||
|
*/
|
||||||
|
{{/enumDescription}}
|
||||||
|
{{{name}}}({{{value}}}){{^-last}},
|
||||||
|
{{/-last}}{{/enumVars}}{{/allowableValues}}
|
||||||
|
|
||||||
|
private final {{{dataType}}} value
|
||||||
|
|
||||||
|
{{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
|
||||||
|
this.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
{{{dataType}}} getValue() {
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String toString() {
|
||||||
|
String.valueOf(value)
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +1,41 @@
|
|||||||
package org.openapitools.api
|
package org.openapitools.api
|
||||||
|
|
||||||
|
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.ToServer
|
||||||
|
|
||||||
import static groovyx.net.http.HttpBuilder.configure
|
import static groovyx.net.http.HttpBuilder.configure
|
||||||
import static java.net.URI.create
|
import static java.net.URI.create
|
||||||
|
|
||||||
class ApiUtils {
|
class ApiUtils {
|
||||||
|
|
||||||
|
static def jsonGenerator = new JsonGenerator.Options()
|
||||||
|
.addConverter(Enum) { Enum u, String key ->
|
||||||
|
u.toString()
|
||||||
|
}
|
||||||
|
.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, contentType, method, container, type) {
|
||||||
def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
|
def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
|
||||||
println "url=$url uriPath=$uriPath"
|
println "url=$url uriPath=$uriPath"
|
||||||
def http = configure {
|
def http = configure {
|
||||||
request.uri = url
|
request.uri = url
|
||||||
request.uri.path = uriPath
|
request.uri.path = uriPath
|
||||||
|
request.encoder(ContentTypes.JSON, { 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());
|
||||||
|
final String json = ((body instanceof String || body instanceof GString)
|
||||||
|
? body.toString()
|
||||||
|
: new JsonBuilder(body, jsonGenerator).toString());
|
||||||
|
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
.invokeMethod(String.valueOf(method).toLowerCase()) {
|
.invokeMethod(String.valueOf(method).toLowerCase()) {
|
||||||
request.uri.query = queryParams
|
request.uri.query = queryParams
|
||||||
|
@ -14,8 +14,33 @@ class Order {
|
|||||||
Integer quantity
|
Integer quantity
|
||||||
|
|
||||||
Date shipDate
|
Date shipDate
|
||||||
|
|
||||||
|
enum StatusEnum {
|
||||||
|
|
||||||
|
PLACED("placed"),
|
||||||
|
|
||||||
|
APPROVED("approved"),
|
||||||
|
|
||||||
|
DELIVERED("delivered")
|
||||||
|
|
||||||
|
private final String value
|
||||||
|
|
||||||
|
StatusEnum(String value) {
|
||||||
|
this.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
String getValue() {
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String toString() {
|
||||||
|
String.valueOf(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Order Status */
|
/* Order Status */
|
||||||
String status
|
StatusEnum status
|
||||||
|
|
||||||
Boolean complete = false
|
Boolean complete = false
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,31 @@ class Pet {
|
|||||||
List<String> photoUrls = new ArrayList<>()
|
List<String> photoUrls = new ArrayList<>()
|
||||||
|
|
||||||
List<Tag> tags
|
List<Tag> tags
|
||||||
/* pet status in the store */
|
|
||||||
String status
|
enum StatusEnum {
|
||||||
|
|
||||||
|
AVAILABLE("available"),
|
||||||
|
|
||||||
|
PENDING("pending"),
|
||||||
|
|
||||||
|
SOLD("sold")
|
||||||
|
|
||||||
|
private final String value
|
||||||
|
|
||||||
|
StatusEnum(String value) {
|
||||||
|
this.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
String getValue() {
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String toString() {
|
||||||
|
String.valueOf(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* pet status in the store */
|
||||||
|
StatusEnum status
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user