From 7c8e55b7616a6d32734b8502158f6240b852ae90 Mon Sep 17 00:00:00 2001 From: BARDEL Julien Date: Thu, 20 Nov 2025 23:15:18 +0100 Subject: [PATCH] [BUG][KOTLIN] Generated enum does not compile if type mapping is not defined #22425 --- bin/configs/kotlin-enum-bigdecimal-value.yaml | 9 ++++ .../languages/AbstractKotlinCodegen.java | 2 + .../kotlin/AbstractKotlinCodegenTest.java | 15 ++++++ .../kotlin/issue-enum-bigdecimal-value.yml | 26 ++++++++++ .../docs/ModelWithEnumBigDecimalValue.md | 17 +++++++ .../models/ModelWithEnumBigDecimalValue.kt | 48 +++++++++++++++++++ .../ModelWithEnumBigDecimalValueTest.kt | 35 ++++++++++++++ 7 files changed, 152 insertions(+) create mode 100644 bin/configs/kotlin-enum-bigdecimal-value.yaml create mode 100644 modules/openapi-generator/src/test/resources/3_0/kotlin/issue-enum-bigdecimal-value.yml create mode 100644 samples/client/petstore/kotlin-enum-bigdecimal-value/docs/ModelWithEnumBigDecimalValue.md create mode 100644 samples/client/petstore/kotlin-enum-bigdecimal-value/src/main/kotlin/org/openapitools/client/models/ModelWithEnumBigDecimalValue.kt create mode 100644 samples/client/petstore/kotlin-enum-bigdecimal-value/src/test/kotlin/org/openapitools/client/models/ModelWithEnumBigDecimalValueTest.kt diff --git a/bin/configs/kotlin-enum-bigdecimal-value.yaml b/bin/configs/kotlin-enum-bigdecimal-value.yaml new file mode 100644 index 000000000000..54ba68e11804 --- /dev/null +++ b/bin/configs/kotlin-enum-bigdecimal-value.yaml @@ -0,0 +1,9 @@ +generatorName: kotlin +outputDir: samples/client/petstore/kotlin-enum-bigdecimal-value +inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue-enum-bigdecimal-value.yml +templateDir: modules/openapi-generator/src/main/resources/kotlin-client +additionalProperties: + artifactId: kotlin-enum-bigdecimal-value + serializationLibrary: gson +globalProperties: + models: "" 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 09b03e1a1f95..0324d4957b6b 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 @@ -883,6 +883,8 @@ public String toEnumValue(String value, String datatype) { } } else if ("kotlin.Float".equals(datatype)) { return value + "f"; + } else if("java.math.BigDecimal".equals(datatype)){ + return value+".toBigDecimal()"; } else { return "\"" + value + "\""; } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java index e9149c073734..1f0289c22d78 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java @@ -99,6 +99,7 @@ public void toEnumValue() { assertEquals(codegen.toEnumValue("1.0", "kotlin.Float"), "1.0f"); assertEquals(codegen.toEnumValue("data", "Something"), "\"data\""); assertEquals(codegen.toEnumValue("data/*", "Something"), "\"data/*\""); + assertEquals(codegen.toEnumValue("1", "java.math.BigDecimal"), "1.toBigDecimal()"); } @Test @@ -382,6 +383,20 @@ public void handleInheritanceWithObjectTypeShouldNotBeAMap() { .fromModel("MapSchema", mapSchema); Assert.assertTrue(mapSchemaModel.isMap); } + + @Test + public void enumIssueWithBigDecimal() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/kotlin/issue-enum-bigdecimal-value.yml"); + + Schema test1 = openAPI.getComponents().getSchemas().get("ModelWithEnumBigDecimalValue"); + CodegenModel cm1 = codegen.fromModel("ModelWithEnumBigDecimalValue", test1); + + codegen.postProcessModels(createCodegenModelWrapper(cm1)); + + //Assert that the generated type is BigDecimal + CodegenProperty cp0 = cm1.vars.get(0); + Assert.assertEquals(cp0.getDataType(), "java.math.BigDecimal"); + } @Test public void handleUseJakartaEeTrue() { diff --git a/modules/openapi-generator/src/test/resources/3_0/kotlin/issue-enum-bigdecimal-value.yml b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue-enum-bigdecimal-value.yml new file mode 100644 index 000000000000..273eb75f04d5 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue-enum-bigdecimal-value.yml @@ -0,0 +1,26 @@ +openapi: 3.0.0 +info: + title: 'Issue enum wrong type' + version: latest +paths: + '/': + get: + operationId: operation + responses: + '200': + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/ModelWithEnumBigDecimalValue' +components: + schemas: + ModelWithEnumBigDecimalValue: + properties: + mode: + type: number + enum: + - -1 + - 0 + - 1.1 + - 0.4 \ No newline at end of file diff --git a/samples/client/petstore/kotlin-enum-bigdecimal-value/docs/ModelWithEnumBigDecimalValue.md b/samples/client/petstore/kotlin-enum-bigdecimal-value/docs/ModelWithEnumBigDecimalValue.md new file mode 100644 index 000000000000..e86fc5b07830 --- /dev/null +++ b/samples/client/petstore/kotlin-enum-bigdecimal-value/docs/ModelWithEnumBigDecimalValue.md @@ -0,0 +1,17 @@ + +# ModelWithEnumBigDecimalValue + +## Properties +| Name | Type | Description | Notes | +| ------------ | ------------- | ------------- | ------------- | +| **mode** | [**inline**](#Mode) | | [optional] | + + + +## Enum: mode +| Name | Value | +| ---- | ----- | +| mode | -1, 0, 1.1, 0.4 | + + + diff --git a/samples/client/petstore/kotlin-enum-bigdecimal-value/src/main/kotlin/org/openapitools/client/models/ModelWithEnumBigDecimalValue.kt b/samples/client/petstore/kotlin-enum-bigdecimal-value/src/main/kotlin/org/openapitools/client/models/ModelWithEnumBigDecimalValue.kt new file mode 100644 index 000000000000..eccbba2384bb --- /dev/null +++ b/samples/client/petstore/kotlin-enum-bigdecimal-value/src/main/kotlin/org/openapitools/client/models/ModelWithEnumBigDecimalValue.kt @@ -0,0 +1,48 @@ +/** + * + * 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 mode + */ + + +data class ModelWithEnumBigDecimalValue ( + + @SerializedName("mode") + val mode: ModelWithEnumBigDecimalValue.Mode? = null + +) { + + /** + * + * + * Values: Minus1,_0,_1Period1,_0Period4 + */ + enum class Mode(val value: java.math.BigDecimal) { + @SerializedName(value = "-1.toBigDecimal()") Minus1(-1.toBigDecimal()), + @SerializedName(value = "0.toBigDecimal()") _0(0.toBigDecimal()), + @SerializedName(value = "1.1.toBigDecimal()") _1Period1(1.1.toBigDecimal()), + @SerializedName(value = "0.4.toBigDecimal()") _0Period4(0.4.toBigDecimal()); + } + +} + diff --git a/samples/client/petstore/kotlin-enum-bigdecimal-value/src/test/kotlin/org/openapitools/client/models/ModelWithEnumBigDecimalValueTest.kt b/samples/client/petstore/kotlin-enum-bigdecimal-value/src/test/kotlin/org/openapitools/client/models/ModelWithEnumBigDecimalValueTest.kt new file mode 100644 index 000000000000..761de5a0cab9 --- /dev/null +++ b/samples/client/petstore/kotlin-enum-bigdecimal-value/src/test/kotlin/org/openapitools/client/models/ModelWithEnumBigDecimalValueTest.kt @@ -0,0 +1,35 @@ +/** + * + * 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 io.kotlintest.shouldBe +import io.kotlintest.specs.ShouldSpec + +import org.openapitools.client.models.ModelWithEnumBigDecimalValue + +class ModelWithEnumBigDecimalValueTest : ShouldSpec() { + init { + // uncomment below to create an instance of ModelWithEnumBigDecimalValue + //val modelInstance = ModelWithEnumBigDecimalValue() + + // to test the property `mode` + should("test mode") { + // uncomment below to test the property + //modelInstance.mode shouldBe ("TODO") + } + + } +}