Skip to content

Commit 2a4e60c

Browse files
[Kotlin] Fix default values (#17937)
* Fixed invalid extraction of response body in kotlin-client jvm-spring-* * Generated echo-api for kotlin-jvm-spring-3-restclient * Specific echo-api for Kotlin without allOf/anyOf * Specific echo-api for Kotlin without allOf/anyOf * Generated all samples * Added kotlin-jvm-spring-3-restclient sample to workflow * Fixed syntax problem * [kotlin] Fixed multiple problems with default values * [kotlin] Removed old commented code * [kotlin] Generated new samples after merge with master * [kotlin] ensure-up-to-date
1 parent 0b02734 commit 2a4e60c

File tree

73 files changed

+4327
-122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+4327
-122
lines changed

.github/workflows/samples-kotlin-client.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ jobs:
6161
- samples/client/petstore/kotlin-jvm-vertx-moshi
6262
- samples/client/petstore/kotlin-jvm-spring-2-webclient
6363
- samples/client/petstore/kotlin-jvm-spring-3-webclient
64+
- samples/client/echo_api/kotlin-jvm-spring-3-webclient
6465
- samples/client/petstore/kotlin-jvm-spring-3-restclient
6566
- samples/client/echo_api/kotlin-jvm-spring-3-restclient
6667
- samples/client/petstore/kotlin-spring-cloud
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
generatorName: kotlin
2+
outputDir: samples/client/echo_api/kotlin-jvm-spring-3-webclient
3+
library: jvm-spring-restclient
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/echo_api.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
6+
additionalProperties:
7+
enumUnknownDefaultCase: true
8+
serializationLibrary: jackson
9+
useSpringBoot3: true

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ public String toEnumVarName(String value, String datatype) {
609609
}
610610

611611
String modified;
612-
if (value.length() == 0) {
612+
if (value.isEmpty()) {
613613
modified = "EMPTY";
614614
} else {
615615
modified = value;
@@ -1044,66 +1044,94 @@ private String fixNumberValue(String number, Schema p) {
10441044
}
10451045

10461046
@Override
1047-
public String toDefaultValue(Schema schema) {
1048-
Schema<?> p = ModelUtils.getReferencedSchema(this.openAPI, schema);
1049-
if (ModelUtils.isBooleanSchema(p)) {
1050-
if (p.getDefault() != null) {
1051-
return p.getDefault().toString();
1047+
public String toDefaultValue(CodegenProperty cp, Schema schema) {
1048+
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
1049+
if (ModelUtils.isBooleanSchema(schema)) {
1050+
if (schema.getDefault() != null) {
1051+
return schema.getDefault().toString();
10521052
}
1053-
} else if (ModelUtils.isDateSchema(p)) {
1053+
} else if (ModelUtils.isDateSchema(schema)) {
10541054
// TODO
10551055
return null;
1056-
} else if (ModelUtils.isDateTimeSchema(p)) {
1056+
} else if (ModelUtils.isDateTimeSchema(schema)) {
10571057
// TODO
10581058
return null;
1059-
} else if (ModelUtils.isNumberSchema(p)) {
1060-
if (p.getDefault() != null) {
1061-
return fixNumberValue(p.getDefault().toString(), p);
1059+
} else if (ModelUtils.isNumberSchema(schema)) {
1060+
if (schema.getDefault() != null) {
1061+
return fixNumberValue(schema.getDefault().toString(), schema);
10621062
}
1063-
} else if (ModelUtils.isIntegerSchema(p)) {
1064-
if (p.getDefault() != null) {
1065-
return fixNumberValue(p.getDefault().toString(), p);
1066-
}
1067-
} else if (ModelUtils.isURISchema(p)) {
1068-
if (p.getDefault() != null) {
1069-
return importMapping.get("URI") + ".create(\"" + p.getDefault() + "\")";
1063+
}
1064+
else if (ModelUtils.isIntegerSchema(schema)) {
1065+
if (schema.getDefault() != null) {
1066+
return fixNumberValue(schema.getDefault().toString(), schema);
10701067
}
1071-
} else if (ModelUtils.isArraySchema(p)) {
1072-
if (p.getDefault() != null) {
1073-
String arrInstantiationType = ModelUtils.isSet(p) ? "set" : "arrayList";
1074-
1075-
if (!(p.getDefault() instanceof ArrayNode)) {
1076-
return null;
1077-
}
1078-
ArrayNode _default = (ArrayNode) p.getDefault();
1079-
if (_default.isEmpty()) {
1080-
return arrInstantiationType + "Of()";
1081-
}
1082-
1083-
StringBuilder defaultContent = new StringBuilder();
1084-
Schema<?> itemsSchema = getSchemaItems((ArraySchema) schema);
1085-
_default.elements().forEachRemaining((element) -> {
1086-
itemsSchema.setDefault(element.asText());
1087-
defaultContent.append(toDefaultValue(itemsSchema)).append(",");
1088-
});
1089-
defaultContent.deleteCharAt(defaultContent.length() - 1); // remove trailing comma
1090-
return arrInstantiationType + "Of(" + defaultContent + ")";
1068+
}
1069+
else if (ModelUtils.isURISchema(schema)) {
1070+
if (schema.getDefault() != null) {
1071+
return importMapping.get("URI") + ".create(\"" + schema.getDefault() + "\")";
10911072
}
1092-
} else if (ModelUtils.isStringSchema(p)) {
1093-
if (p.getDefault() != null) {
1094-
String _default = String.valueOf(p.getDefault());
1095-
if (p.getEnum() == null) {
1073+
}
1074+
else if (ModelUtils.isArraySchema(schema)) {
1075+
return toArrayDefaultValue(cp, schema);
1076+
} else if (ModelUtils.isStringSchema(schema)) {
1077+
if (schema.getDefault() != null) {
1078+
String _default = String.valueOf(schema.getDefault());
1079+
if (schema.getEnum() == null) {
10961080
return "\"" + escapeText(_default) + "\"";
10971081
} else {
10981082
// convert to enum var name later in postProcessModels
10991083
return _default;
11001084
}
11011085
}
11021086
return null;
1087+
} else if (ModelUtils.isObjectSchema(schema)) {
1088+
if (schema.getDefault() != null) {
1089+
return super.toDefaultValue(schema);
1090+
}
1091+
return null;
1092+
}
1093+
return null;
1094+
}
1095+
1096+
private String toArrayDefaultValue(CodegenProperty cp, Schema schema) {
1097+
if (schema.getDefault() != null) {
1098+
String arrInstantiationType = ModelUtils.isSet(schema) ? "set" : "arrayList";
1099+
1100+
if (!(schema.getDefault() instanceof ArrayNode)) {
1101+
return null;
1102+
}
1103+
ArrayNode _default = (ArrayNode) schema.getDefault();
1104+
if (_default.isEmpty()) {
1105+
return arrInstantiationType + "Of()";
1106+
}
1107+
1108+
StringBuilder defaultContent = new StringBuilder();
1109+
Schema<?> itemsSchema = getSchemaItems((ArraySchema) schema);
1110+
_default.elements().forEachRemaining((element) -> {
1111+
String defaultValue = element.asText();
1112+
if (defaultValue != null) {
1113+
if (cp.items.getIsEnumOrRef()) {
1114+
String className = cp.items.datatypeWithEnum;
1115+
String enumVarName = toEnumVarName(defaultValue, cp.items.dataType);
1116+
defaultContent.append(className).append(".").append(enumVarName).append(",");
1117+
} else {
1118+
itemsSchema.setDefault(defaultValue);
1119+
defaultValue = toDefaultValue(cp, itemsSchema);
1120+
defaultContent.append(defaultValue).append(",");
1121+
}
1122+
}
1123+
});
1124+
defaultContent.deleteCharAt(defaultContent.length() - 1); // remove trailing comma
1125+
return arrInstantiationType + "Of(" + defaultContent + ")";
11031126
}
11041127
return null;
11051128
}
11061129

1130+
@Override
1131+
public String toDefaultParameterValue(CodegenProperty cp, Schema schema) {
1132+
return toDefaultValue(cp, schema);
1133+
}
1134+
11071135
@Override
11081136
public GeneratorLanguage generatorLanguage() {
11091137
return GeneratorLanguage.KOTLIN;

modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-spring-restclient/api.mustache

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,28 @@ import {{packageName}}.infrastructure.*
3636
/**
3737
* enum for parameter {{paramName}}
3838
*/
39-
{{#nonPublicApi}}internal {{/nonPublicApi}}enum class {{enumName}}{{operationIdCamelCase}}(val value: {{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}kotlin.String{{/isContainer}}) {
40-
{{^enumUnknownDefaultCase}}
41-
{{#allowableValues}}
42-
{{#enumVars}}
43-
{{#jackson}}
44-
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
45-
{{/jackson}}
46-
{{/enumVars}}
47-
{{/allowableValues}}
48-
{{/enumUnknownDefaultCase}}
49-
{{#enumUnknownDefaultCase}}
50-
{{#allowableValues}}
51-
{{#enumVars}}
52-
{{^-last}}
53-
{{#jackson}}
54-
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}),
55-
{{/jackson}}
56-
{{/-last}}
57-
{{/enumVars}}
58-
{{/allowableValues}}
59-
{{/enumUnknownDefaultCase}}
60-
}
39+
{{#nonPublicApi}}internal {{/nonPublicApi}}enum class {{enumName}}{{operationIdCamelCase}}(val value: {{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}kotlin.String{{/isContainer}}) {
40+
{{^enumUnknownDefaultCase}}
41+
{{#allowableValues}}
42+
{{#enumVars}}
43+
{{#jackson}}
44+
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
45+
{{/jackson}}
46+
{{/enumVars}}
47+
{{/allowableValues}}
48+
{{/enumUnknownDefaultCase}}
49+
{{#enumUnknownDefaultCase}}
50+
{{#allowableValues}}
51+
{{#enumVars}}
52+
{{^-last}}
53+
{{#jackson}}
54+
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}),
55+
{{/jackson}}
56+
{{/-last}}
57+
{{/enumVars}}
58+
{{/allowableValues}}
59+
{{/enumUnknownDefaultCase}}
60+
}
6161

6262
{{/isEnum}}
6363
{{/allParams}}
@@ -66,7 +66,7 @@ import {{packageName}}.infrastructure.*
6666
{{#isDeprecated}}
6767
@Deprecated(message = "This operation is deprecated.")
6868
{{/isDeprecated}}
69-
fun {{operationId}}({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{#defaultValue}} = {{^isNumber}}{{#isEnum}}{{enumName}}{{operationIdCamelCase}}.{{enumDefaultValue}}{{/isEnum}}{{^isEnum}}{{{defaultValue}}}{{/isEnum}}{{/isNumber}}{{#isNumber}}{{{defaultValue}}}.toDouble(){{/isNumber}}{{/defaultValue}}{{^defaultValue}} = null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): {{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
69+
fun {{operationId}}({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{#defaultValue}} = {{>param_default_value}}{{/defaultValue}}{{^defaultValue}} = null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): {{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}} {
7070
val result = {{operationId}}WithHttpInfo({{#allParams}}{{{paramName}}} = {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}})
7171
{{#returnType}}
7272
return result.body!!
@@ -77,7 +77,7 @@ import {{packageName}}.infrastructure.*
7777
{{#isDeprecated}}
7878
@Deprecated(message = "This operation is deprecated.")
7979
{{/isDeprecated}}
80-
fun {{operationId}}WithHttpInfo({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{#defaultValue}} = {{^isNumber}}{{#isEnum}}{{enumName}}{{operationIdCamelCase}}.{{enumDefaultValue}}{{/isEnum}}{{^isEnum}}{{{defaultValue}}}{{/isEnum}}{{/isNumber}}{{#isNumber}}{{{defaultValue}}}.toDouble(){{/isNumber}}{{/defaultValue}}{{^defaultValue}} = null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): ResponseEntity<{{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}> {
80+
fun {{operationId}}WithHttpInfo({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{#defaultValue}} = {{>param_default_value}}{{/defaultValue}}{{^defaultValue}} = null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): ResponseEntity<{{#returnType}}{{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}> {
8181
val localVariableConfig = {{operationId}}RequestConfig({{#allParams}}{{{paramName}}} = {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}})
8282
return request<{{#hasBodyParam}}{{#bodyParams}}{{{dataType}}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}Unit{{/hasFormParams}}{{#hasFormParams}}Map<String, PartConfig<*>>{{/hasFormParams}}{{/hasBodyParam}}, {{{returnType}}}{{^returnType}}Unit{{/returnType}}>(
8383
localVariableConfig
@@ -87,7 +87,7 @@ import {{packageName}}.infrastructure.*
8787
{{#isDeprecated}}
8888
@Deprecated(message = "This operation is deprecated.")
8989
{{/isDeprecated}}
90-
fun {{operationId}}RequestConfig({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{#defaultValue}} = {{^isNumber}}{{#isEnum}}{{enumName}}{{operationIdCamelCase}}.{{enumDefaultValue}}{{/isEnum}}{{^isEnum}}{{{defaultValue}}}{{/isEnum}}{{/isNumber}}{{#isNumber}}{{{defaultValue}}}.toDouble(){{/isNumber}}{{/defaultValue}}{{^defaultValue}} = null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) : RequestConfig<{{#hasBodyParam}}{{#bodyParams}}{{{dataType}}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}Unit{{/hasFormParams}}{{#hasFormParams}}Map<String, PartConfig<*>>{{/hasFormParams}}{{/hasBodyParam}}> {
90+
fun {{operationId}}RequestConfig({{#allParams}}{{{paramName}}}: {{#isEnum}}{{#isContainer}}kotlin.collections.List<{{enumName}}{{operationIdCamelCase}}>{{/isContainer}}{{^isContainer}}{{enumName}}{{operationIdCamelCase}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}?{{#defaultValue}} = {{>param_default_value}}{{/defaultValue}}{{^defaultValue}} = null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) : RequestConfig<{{#hasBodyParam}}{{#bodyParams}}{{{dataType}}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{^hasFormParams}}Unit{{/hasFormParams}}{{#hasFormParams}}Map<String, PartConfig<*>>{{/hasFormParams}}{{/hasBodyParam}}> {
9191
val localVariableBody = {{#hasBodyParam}}{{!
9292
}}{{#bodyParams}}{{{paramName}}}{{/bodyParams}}{{/hasBodyParam}}{{^hasBodyParam}}{{!
9393
}}{{^hasFormParams}}null{{/hasFormParams}}{{!

0 commit comments

Comments
 (0)