Skip to content

Commit

Permalink
[Kotlin-Spring] Fix properties default value (#8373)
Browse files Browse the repository at this point in the history
* [Kotlin-Spring] Fix properties default value

* Fix kotlin enum default

* Update go sample
  • Loading branch information
cbornet authored Jan 24, 2021
1 parent c96764f commit 06ad7a5
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5357,7 +5357,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
if (var.defaultValue != null) {
String enumName = null;
final String enumDefaultValue;
if ("string".equalsIgnoreCase(dataType)) {
if (isDataTypeString(dataType)) {
enumDefaultValue = toEnumValue(var.defaultValue, dataType);
} else {
enumDefaultValue = var.defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,8 @@ public void postProcessFile(File file, String fileType) {
}

@Override
public String toDefaultValue(Schema p) {
public String toDefaultValue(Schema schema) {
Schema p = ModelUtils.getReferencedSchema(this.openAPI, schema);
if (ModelUtils.isBooleanSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
Expand All @@ -921,8 +922,15 @@ public String toDefaultValue(Schema p) {
}
} else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) {
return "\"" + p.getDefault() + "\"";
String _default = (String) p.getDefault();
if (p.getEnum() == null) {
return "\"" + escapeText(_default) + "\"";
} else {
// convert to enum var name later in postProcessModels
return _default;
}
}
return null;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}{{#swaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}{{#deprecated}}
@Deprecated(message = ""){{/deprecated}}
@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}}
@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}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#useBeanValidation}}{{>beanValidation}}{{>beanValidationModel}}{{/useBeanValidation}}{{#swaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}required = true, {{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}
@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}}
@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}}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Name | Type | Description | Notes
**EnumNumber** | Pointer to **float64** | | [optional]
**OuterEnum** | Pointer to [**NullableOuterEnum**](OuterEnum.md) | | [optional]
**OuterEnumInteger** | Pointer to [**OuterEnumInteger**](OuterEnumInteger.md) | | [optional]
**OuterEnumDefaultValue** | Pointer to [**OuterEnumDefaultValue**](OuterEnumDefaultValue.md) | | [optional] [default to "placed"]
**OuterEnumDefaultValue** | Pointer to [**OuterEnumDefaultValue**](OuterEnumDefaultValue.md) | | [optional] [default to OUTERENUMDEFAULTVALUE_PLACED]
**OuterEnumIntegerDefaultValue** | Pointer to [**OuterEnumIntegerDefaultValue**](OuterEnumIntegerDefaultValue.md) | | [optional] [default to OUTERENUMINTEGERDEFAULTVALUE__0]

## Methods
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ data class Order(
@field:JsonProperty("status") val status: Order.Status? = null,

@ApiModelProperty(example = "null", value = "")
@field:JsonProperty("complete") val complete: kotlin.Boolean? = null
@field:JsonProperty("complete") val complete: kotlin.Boolean? = false
) {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ data class Order(
@field:JsonProperty("status") val status: Order.Status? = null,

@ApiModelProperty(example = "null", value = "")
@field:JsonProperty("complete") val complete: kotlin.Boolean? = null
@field:JsonProperty("complete") val complete: kotlin.Boolean? = false
) {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ data class Order(
@field:JsonProperty("status") val status: Order.Status? = null,

@ApiModelProperty(example = "null", value = "")
@field:JsonProperty("complete") val complete: kotlin.Boolean? = null
@field:JsonProperty("complete") val complete: kotlin.Boolean? = false
) {

/**
Expand Down

0 comments on commit 06ad7a5

Please sign in to comment.