Skip to content

Commit 7c8e55b

Browse files
committed
[BUG][KOTLIN] Generated enum does not compile if type mapping is not defined #22425
1 parent 4a7e0c9 commit 7c8e55b

File tree

7 files changed

+152
-0
lines changed

7 files changed

+152
-0
lines changed
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/petstore/kotlin-enum-bigdecimal-value
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue-enum-bigdecimal-value.yml
4+
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
5+
additionalProperties:
6+
artifactId: kotlin-enum-bigdecimal-value
7+
serializationLibrary: gson
8+
globalProperties:
9+
models: ""

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ public String toEnumValue(String value, String datatype) {
883883
}
884884
} else if ("kotlin.Float".equals(datatype)) {
885885
return value + "f";
886+
} else if("java.math.BigDecimal".equals(datatype)){
887+
return value+".toBigDecimal()";
886888
} else {
887889
return "\"" + value + "\"";
888890
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public void toEnumValue() {
9999
assertEquals(codegen.toEnumValue("1.0", "kotlin.Float"), "1.0f");
100100
assertEquals(codegen.toEnumValue("data", "Something"), "\"data\"");
101101
assertEquals(codegen.toEnumValue("data/*", "Something"), "\"data/*\"");
102+
assertEquals(codegen.toEnumValue("1", "java.math.BigDecimal"), "1.toBigDecimal()");
102103
}
103104

104105
@Test
@@ -382,6 +383,20 @@ public void handleInheritanceWithObjectTypeShouldNotBeAMap() {
382383
.fromModel("MapSchema", mapSchema);
383384
Assert.assertTrue(mapSchemaModel.isMap);
384385
}
386+
387+
@Test
388+
public void enumIssueWithBigDecimal() {
389+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/kotlin/issue-enum-bigdecimal-value.yml");
390+
391+
Schema test1 = openAPI.getComponents().getSchemas().get("ModelWithEnumBigDecimalValue");
392+
CodegenModel cm1 = codegen.fromModel("ModelWithEnumBigDecimalValue", test1);
393+
394+
codegen.postProcessModels(createCodegenModelWrapper(cm1));
395+
396+
//Assert that the generated type is BigDecimal
397+
CodegenProperty cp0 = cm1.vars.get(0);
398+
Assert.assertEquals(cp0.getDataType(), "java.math.BigDecimal");
399+
}
385400

386401
@Test
387402
public void handleUseJakartaEeTrue() {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: 3.0.0
2+
info:
3+
title: 'Issue enum wrong type'
4+
version: latest
5+
paths:
6+
'/':
7+
get:
8+
operationId: operation
9+
responses:
10+
'200':
11+
description: Success
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '#/components/schemas/ModelWithEnumBigDecimalValue'
16+
components:
17+
schemas:
18+
ModelWithEnumBigDecimalValue:
19+
properties:
20+
mode:
21+
type: number
22+
enum:
23+
- -1
24+
- 0
25+
- 1.1
26+
- 0.4
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# ModelWithEnumBigDecimalValue
3+
4+
## Properties
5+
| Name | Type | Description | Notes |
6+
| ------------ | ------------- | ------------- | ------------- |
7+
| **mode** | [**inline**](#Mode) | | [optional] |
8+
9+
10+
<a id="Mode"></a>
11+
## Enum: mode
12+
| Name | Value |
13+
| ---- | ----- |
14+
| mode | -1, 0, 1.1, 0.4 |
15+
16+
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
*
3+
* Please note:
4+
* This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
5+
* Do not edit this file manually.
6+
*
7+
*/
8+
9+
@file:Suppress(
10+
"ArrayInDataClass",
11+
"EnumEntryName",
12+
"RemoveRedundantQualifierName",
13+
"UnusedImport"
14+
)
15+
16+
package org.openapitools.client.models
17+
18+
19+
import com.google.gson.annotations.SerializedName
20+
21+
/**
22+
*
23+
*
24+
* @param mode
25+
*/
26+
27+
28+
data class ModelWithEnumBigDecimalValue (
29+
30+
@SerializedName("mode")
31+
val mode: ModelWithEnumBigDecimalValue.Mode? = null
32+
33+
) {
34+
35+
/**
36+
*
37+
*
38+
* Values: Minus1,_0,_1Period1,_0Period4
39+
*/
40+
enum class Mode(val value: java.math.BigDecimal) {
41+
@SerializedName(value = "-1.toBigDecimal()") Minus1(-1.toBigDecimal()),
42+
@SerializedName(value = "0.toBigDecimal()") _0(0.toBigDecimal()),
43+
@SerializedName(value = "1.1.toBigDecimal()") _1Period1(1.1.toBigDecimal()),
44+
@SerializedName(value = "0.4.toBigDecimal()") _0Period4(0.4.toBigDecimal());
45+
}
46+
47+
}
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*
3+
* Please note:
4+
* This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
5+
* Do not edit this file manually.
6+
*
7+
*/
8+
9+
@file:Suppress(
10+
"ArrayInDataClass",
11+
"EnumEntryName",
12+
"RemoveRedundantQualifierName",
13+
"UnusedImport"
14+
)
15+
16+
package org.openapitools.client.models
17+
18+
import io.kotlintest.shouldBe
19+
import io.kotlintest.specs.ShouldSpec
20+
21+
import org.openapitools.client.models.ModelWithEnumBigDecimalValue
22+
23+
class ModelWithEnumBigDecimalValueTest : ShouldSpec() {
24+
init {
25+
// uncomment below to create an instance of ModelWithEnumBigDecimalValue
26+
//val modelInstance = ModelWithEnumBigDecimalValue()
27+
28+
// to test the property `mode`
29+
should("test mode") {
30+
// uncomment below to test the property
31+
//modelInstance.mode shouldBe ("TODO")
32+
}
33+
34+
}
35+
}

0 commit comments

Comments
 (0)