Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[groovy] support enum generation in groovy client #15619

Merged
merged 5 commits into from
May 24, 2023
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: 0 additions & 1 deletion docs/generators/groovy.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|discriminatorCaseSensitive|Whether the discriminator value lookup should be case-sensitive or not. This option only works for Java API client| |true|
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
|fullJavaUtil|whether to use fully qualified name for classes under java.util. This option only works for Java API client| |false|
|groupId|groupId in generated pom.xml| |org.openapitools|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |false|
|ignoreAnyOfInEnum|Ignore anyOf keyword in enum| |false|
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Original file line number Diff line number Diff line change
@@ -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}}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ class Pet {
List<String> photoUrls = new ArrayList<>()

List<Tag> 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
}