diff --git a/bin/configs/kotlin-default-values-numbers.yaml b/bin/configs/kotlin-default-values-numbers.yaml new file mode 100644 index 00000000000..1a76dd1dfde --- /dev/null +++ b/bin/configs/kotlin-default-values-numbers.yaml @@ -0,0 +1,9 @@ +generatorName: kotlin +outputDir: samples/client/petstore/kotlin-default-values-numbers +inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue13506-defaultValue-numbers.yaml +templateDir: modules/openapi-generator/src/main/resources/kotlin-client +additionalProperties: + artifactId: kotlin-default-values-numbers + serializationLibrary: gson +globalProperties: + models: "" \ No newline at end of file diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java index a06268e50ed..fbcfed48106 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java @@ -1001,6 +1001,20 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co } } + private String fixNumberValue(String number, Schema p) { + if (ModelUtils.isFloatSchema(p)) { + return number + "f"; + } else if (ModelUtils.isDoubleSchema(p)) { + if (number.contains(".")) { + return number; + } + return number + ".0"; + } else if (ModelUtils.isLongSchema(p)) { + return number + "L"; + } + return number; + } + @Override public String toDefaultValue(Schema schema) { Schema p = ModelUtils.getReferencedSchema(this.openAPI, schema); @@ -1014,11 +1028,11 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co // TODO } else if (ModelUtils.isNumberSchema(p)) { if (p.getDefault() != null) { - return p.getDefault().toString(); + return fixNumberValue(p.getDefault().toString(), p); } } else if (ModelUtils.isIntegerSchema(p)) { if (p.getDefault() != null) { - return p.getDefault().toString(); + return fixNumberValue(p.getDefault().toString(), p); } } else if (ModelUtils.isURISchema(p)) { if (p.getDefault() != null) { diff --git a/modules/openapi-generator/src/test/resources/3_0/kotlin/issue13506-defaultValue-numbers.yaml b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue13506-defaultValue-numbers.yaml new file mode 100644 index 00000000000..8ee94eaad2b --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue13506-defaultValue-numbers.yaml @@ -0,0 +1,51 @@ +openapi: 3.0.0 +info: + title: 'Issue X default value number with format' + version: latest +paths: + '/': + get: + operationId: operation + responses: + '200': + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/ModelWithPropertyHavingDefault' +components: + schemas: + ModelWithPropertyHavingDefault: + properties: + propertyInt: + type: integer + default: 0 + format: int32 + propertyLong: + type: integer + default: 0 + format: int64 + propertyFloat1: + type: number + default: 0 + format: float + propertyFloat2: + type: number + default: 0.0 + format: float + propertyFloat3: + type: number + default: 0.01 + format: float + propertyDouble1: + type: number + default: 0 + format: double + propertyDouble2: + type: number + default: 0.0 + format: double + propertyDouble3: + type: number + default: 0.01 + format: double diff --git a/samples/client/petstore/kotlin-default-values-numbers/docs/ModelWithPropertyHavingDefault.md b/samples/client/petstore/kotlin-default-values-numbers/docs/ModelWithPropertyHavingDefault.md new file mode 100644 index 00000000000..0d41a58b198 --- /dev/null +++ b/samples/client/petstore/kotlin-default-values-numbers/docs/ModelWithPropertyHavingDefault.md @@ -0,0 +1,17 @@ + +# ModelWithPropertyHavingDefault + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**propertyInt** | **kotlin.Int** | | [optional] +**propertyLong** | **kotlin.Long** | | [optional] +**propertyFloat1** | **kotlin.Float** | | [optional] +**propertyFloat2** | **kotlin.Float** | | [optional] +**propertyFloat3** | **kotlin.Float** | | [optional] +**propertyDouble1** | **kotlin.Double** | | [optional] +**propertyDouble2** | **kotlin.Double** | | [optional] +**propertyDouble3** | **kotlin.Double** | | [optional] + + + diff --git a/samples/client/petstore/kotlin-default-values-numbers/src/main/kotlin/org/openapitools/client/models/ModelWithPropertyHavingDefault.kt b/samples/client/petstore/kotlin-default-values-numbers/src/main/kotlin/org/openapitools/client/models/ModelWithPropertyHavingDefault.kt new file mode 100644 index 00000000000..6894443aa69 --- /dev/null +++ b/samples/client/petstore/kotlin-default-values-numbers/src/main/kotlin/org/openapitools/client/models/ModelWithPropertyHavingDefault.kt @@ -0,0 +1,66 @@ +/** + * Issue X default value number with format + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * The version of the OpenAPI document: latest + * + * + * Please note: + * This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * Do not edit this file manually. + */ + +@file:Suppress( + "ArrayInDataClass", + "EnumEntryName", + "RemoveRedundantQualifierName", + "UnusedImport" +) + +package org.openapitools.client.models + + +import com.google.gson.annotations.SerializedName + +/** + * + * + * @param propertyInt + * @param propertyLong + * @param propertyFloat1 + * @param propertyFloat2 + * @param propertyFloat3 + * @param propertyDouble1 + * @param propertyDouble2 + * @param propertyDouble3 + */ + +data class ModelWithPropertyHavingDefault ( + + @SerializedName("propertyInt") + val propertyInt: kotlin.Int? = 0, + + @SerializedName("propertyLong") + val propertyLong: kotlin.Long? = 0L, + + @SerializedName("propertyFloat1") + val propertyFloat1: kotlin.Float? = 0f, + + @SerializedName("propertyFloat2") + val propertyFloat2: kotlin.Float? = 0.0f, + + @SerializedName("propertyFloat3") + val propertyFloat3: kotlin.Float? = 0.01f, + + @SerializedName("propertyDouble1") + val propertyDouble1: kotlin.Double? = 0.0, + + @SerializedName("propertyDouble2") + val propertyDouble2: kotlin.Double? = 0.0, + + @SerializedName("propertyDouble3") + val propertyDouble3: kotlin.Double? = 0.01 + +) +