From 5b5cb1f7e0d56b1e120e81cbf2ff74a167766de7 Mon Sep 17 00:00:00 2001 From: George Date: Wed, 24 May 2023 11:12:59 +0400 Subject: [PATCH] [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 --- .../main/resources/Groovy/ApiUtils.mustache | 25 +++++++++++++++++ .../src/main/resources/Groovy/model.mustache | 8 +----- .../main/resources/Groovy/modelClass.mustache | 21 +++++++++++++++ .../main/resources/Groovy/modelEnum.mustache | 25 +++++++++++++++++ .../org/openapitools/api/ApiUtils.groovy | 25 +++++++++++++++++ .../org/openapitools/model/Order.groovy | 27 ++++++++++++++++++- .../groovy/org/openapitools/model/Pet.groovy | 27 ++++++++++++++++++- 7 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/Groovy/modelClass.mustache create mode 100644 modules/openapi-generator/src/main/resources/Groovy/modelEnum.mustache diff --git a/modules/openapi-generator/src/main/resources/Groovy/ApiUtils.mustache b/modules/openapi-generator/src/main/resources/Groovy/ApiUtils.mustache index 95070ee3e73..3acb16972e2 100644 --- a/modules/openapi-generator/src/main/resources/Groovy/ApiUtils.mustache +++ b/modules/openapi-generator/src/main/resources/Groovy/ApiUtils.mustache @@ -1,16 +1,41 @@ 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 java.net.URI.create 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) { 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(); + 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()) { request.uri.query = queryParams diff --git a/modules/openapi-generator/src/main/resources/Groovy/model.mustache b/modules/openapi-generator/src/main/resources/Groovy/model.mustache index 00cedcfd15a..1c304c98d92 100644 --- a/modules/openapi-generator/src/main/resources/Groovy/model.mustache +++ b/modules/openapi-generator/src/main/resources/Groovy/model.mustache @@ -7,12 +7,6 @@ import {{import}}; {{#models}} {{#model}} -@Canonical -class {{classname}} { - {{#vars}} - {{#description}}/* {{{.}}} */{{/description}} - {{{dataType}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}} - {{/vars}} -} +{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelClass}}{{/isEnum}} {{/model}} {{/models}} diff --git a/modules/openapi-generator/src/main/resources/Groovy/modelClass.mustache b/modules/openapi-generator/src/main/resources/Groovy/modelClass.mustache new file mode 100644 index 00000000000..39c0c6aa384 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Groovy/modelClass.mustache @@ -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}} +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/Groovy/modelEnum.mustache b/modules/openapi-generator/src/main/resources/Groovy/modelEnum.mustache new file mode 100644 index 00000000000..493be4503d1 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Groovy/modelEnum.mustache @@ -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) + } +} \ No newline at end of file diff --git a/samples/client/petstore/groovy/src/main/groovy/org/openapitools/api/ApiUtils.groovy b/samples/client/petstore/groovy/src/main/groovy/org/openapitools/api/ApiUtils.groovy index c1fe94b1027..cc4d3d248e5 100644 --- a/samples/client/petstore/groovy/src/main/groovy/org/openapitools/api/ApiUtils.groovy +++ b/samples/client/petstore/groovy/src/main/groovy/org/openapitools/api/ApiUtils.groovy @@ -1,16 +1,41 @@ 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 java.net.URI.create 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) { 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(); + 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()) { request.uri.query = queryParams diff --git a/samples/client/petstore/groovy/src/main/groovy/org/openapitools/model/Order.groovy b/samples/client/petstore/groovy/src/main/groovy/org/openapitools/model/Order.groovy index d9e02f8a6cc..fb00b075b84 100644 --- a/samples/client/petstore/groovy/src/main/groovy/org/openapitools/model/Order.groovy +++ b/samples/client/petstore/groovy/src/main/groovy/org/openapitools/model/Order.groovy @@ -14,8 +14,33 @@ class Order { Integer quantity 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 */ - String status + StatusEnum status Boolean complete = false } diff --git a/samples/client/petstore/groovy/src/main/groovy/org/openapitools/model/Pet.groovy b/samples/client/petstore/groovy/src/main/groovy/org/openapitools/model/Pet.groovy index c557a4008cb..eb432e20996 100644 --- a/samples/client/petstore/groovy/src/main/groovy/org/openapitools/model/Pet.groovy +++ b/samples/client/petstore/groovy/src/main/groovy/org/openapitools/model/Pet.groovy @@ -20,6 +20,31 @@ class Pet { List photoUrls = new ArrayList<>() List tags + + 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 */ - String status + StatusEnum status }