Skip to content

Commit 06ad7a5

Browse files
authored
[Kotlin-Spring] Fix properties default value (#8373)
* [Kotlin-Spring] Fix properties default value * Fix kotlin enum default * Update go sample
1 parent c96764f commit 06ad7a5

File tree

9 files changed

+19
-11
lines changed

9 files changed

+19
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5357,7 +5357,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
53575357
if (var.defaultValue != null) {
53585358
String enumName = null;
53595359
final String enumDefaultValue;
5360-
if ("string".equalsIgnoreCase(dataType)) {
5360+
if (isDataTypeString(dataType)) {
53615361
enumDefaultValue = toEnumValue(var.defaultValue, dataType);
53625362
} else {
53635363
enumDefaultValue = var.defaultValue;

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,8 @@ public void postProcessFile(File file, String fileType) {
898898
}
899899

900900
@Override
901-
public String toDefaultValue(Schema p) {
901+
public String toDefaultValue(Schema schema) {
902+
Schema p = ModelUtils.getReferencedSchema(this.openAPI, schema);
902903
if (ModelUtils.isBooleanSchema(p)) {
903904
if (p.getDefault() != null) {
904905
return p.getDefault().toString();
@@ -921,8 +922,15 @@ public String toDefaultValue(Schema p) {
921922
}
922923
} else if (ModelUtils.isStringSchema(p)) {
923924
if (p.getDefault() != null) {
924-
return "\"" + p.getDefault() + "\"";
925+
String _default = (String) p.getDefault();
926+
if (p.getEnum() == null) {
927+
return "\"" + escapeText(_default) + "\"";
928+
} else {
929+
// convert to enum var name later in postProcessModels
930+
return _default;
931+
}
925932
}
933+
return null;
926934
}
927935

928936
return null;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}{{#swaggerAnnotations}}
22
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}{{#deprecated}}
33
@Deprecated(message = ""){{/deprecated}}
4-
@field:JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{nameInCamelCase}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
4+
@field:JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{nameInCamelCase}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultValue}}{{defaultValue}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}{{#swaggerAnnotations}}
22
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}required = true, {{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}
3-
@field:JsonProperty("{{{baseName}}}", required = true){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{nameInCamelCase}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isReadOnly}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}{{/isReadOnly}}
3+
@field:JsonProperty("{{{baseName}}}", required = true){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isArray}}{{baseType}}<{{/isArray}}{{classname}}.{{nameInCamelCase}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isReadOnly}}?{{/isReadOnly}}{{#defaultValue}} = {{defaultValue}}{{/defaultValue}}{{#isReadOnly}}{{^defaultValue}} = null{{/defaultValue}}{{/isReadOnly}}

samples/openapi3/client/petstore/go/go-petstore/docs/EnumTest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Name | Type | Description | Notes
1010
**EnumNumber** | Pointer to **float64** | | [optional]
1111
**OuterEnum** | Pointer to [**NullableOuterEnum**](OuterEnum.md) | | [optional]
1212
**OuterEnumInteger** | Pointer to [**OuterEnumInteger**](OuterEnumInteger.md) | | [optional]
13-
**OuterEnumDefaultValue** | Pointer to [**OuterEnumDefaultValue**](OuterEnumDefaultValue.md) | | [optional] [default to "placed"]
13+
**OuterEnumDefaultValue** | Pointer to [**OuterEnumDefaultValue**](OuterEnumDefaultValue.md) | | [optional] [default to OUTERENUMDEFAULTVALUE_PLACED]
1414
**OuterEnumIntegerDefaultValue** | Pointer to [**OuterEnumIntegerDefaultValue**](OuterEnumIntegerDefaultValue.md) | | [optional] [default to OUTERENUMINTEGERDEFAULTVALUE__0]
1515

1616
## Methods

samples/openapi3/client/petstore/go/go-petstore/model_enum_test_.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/server/petstore/kotlin-springboot-delegate/src/main/kotlin/org/openapitools/model/Order.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ data class Order(
4040
@field:JsonProperty("status") val status: Order.Status? = null,
4141

4242
@ApiModelProperty(example = "null", value = "")
43-
@field:JsonProperty("complete") val complete: kotlin.Boolean? = null
43+
@field:JsonProperty("complete") val complete: kotlin.Boolean? = false
4444
) {
4545

4646
/**

samples/server/petstore/kotlin-springboot-reactive/src/main/kotlin/org/openapitools/model/Order.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ data class Order(
4040
@field:JsonProperty("status") val status: Order.Status? = null,
4141

4242
@ApiModelProperty(example = "null", value = "")
43-
@field:JsonProperty("complete") val complete: kotlin.Boolean? = null
43+
@field:JsonProperty("complete") val complete: kotlin.Boolean? = false
4444
) {
4545

4646
/**

samples/server/petstore/kotlin-springboot/src/main/kotlin/org/openapitools/model/Order.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ data class Order(
4040
@field:JsonProperty("status") val status: Order.Status? = null,
4141

4242
@ApiModelProperty(example = "null", value = "")
43-
@field:JsonProperty("complete") val complete: kotlin.Boolean? = null
43+
@field:JsonProperty("complete") val complete: kotlin.Boolean? = false
4444
) {
4545

4646
/**

0 commit comments

Comments
 (0)