Skip to content

Commit 7b535a9

Browse files
Fix Kotlin boolean const enum literals (OpenAPITools#24022)
1 parent ef47165 commit 7b535a9

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,8 @@ public CodegenModel fromModel(String name, Schema schema) {
10521052

10531053
@Override
10541054
public String toEnumValue(String value, String datatype) {
1055-
if ("kotlin.Int".equals(datatype) || "kotlin.Long".equals(datatype)) {
1055+
if ("kotlin.Int".equals(datatype) || "kotlin.Long".equals(datatype)
1056+
|| "kotlin.Boolean".equals(datatype)) {
10561057
return value;
10571058
} else if ("kotlin.Double".equals(datatype)) {
10581059
if (value.contains(".")) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public void toEnumValue() {
172172
assertEquals(codegen.toEnumValue("1", "kotlin.Double"), "1.0");
173173
assertEquals(codegen.toEnumValue("1.3", "kotlin.Double"), "1.3");
174174
assertEquals(codegen.toEnumValue("1337", "kotlin.Long"), "1337");
175+
assertEquals(codegen.toEnumValue("true", "kotlin.Boolean"), "true");
175176
assertEquals(codegen.toEnumValue("5", "kotlin.Float"), "5f");
176177
assertEquals(codegen.toEnumValue("1.0", "kotlin.Float"), "1.0f");
177178
assertEquals(codegen.toEnumValue("data", "Something"), "\"data\"");

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,36 @@ public void testIntArrayToEnum() throws IOException {
818818
TestUtils.assertFileContains(modelKt, "enum class DaysOfWeek(val value: kotlin.Int)");
819819
}
820820

821+
@Test
822+
public void testBooleanConstEnumUsesBooleanLiteral() throws IOException {
823+
File output = Files.createTempDirectory("test").toFile();
824+
output.deleteOnExit();
825+
826+
final CodegenConfigurator configurator = new CodegenConfigurator()
827+
.setGeneratorName("kotlin")
828+
.setLibrary("jvm-ktor")
829+
.setAdditionalProperties(new HashMap<>() {{
830+
put(CodegenConstants.SERIALIZATION_LIBRARY, "jackson");
831+
put(CodegenConstants.MODEL_PACKAGE, "model");
832+
put(ENUM_PROPERTY_NAMING, "original");
833+
}})
834+
.setInputSpec("src/test/resources/3_1/kotlin/issue23550-boolean-const.yaml")
835+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
836+
837+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
838+
DefaultGenerator generator = new DefaultGenerator();
839+
840+
generator.opts(clientOptInput).generate();
841+
842+
final Path modelKt = Paths.get(output + "/src/main/kotlin/model/ExceptionState.kt");
843+
844+
TestUtils.assertFileContains(modelKt,
845+
"enum class ExceptionPeriodIsClosed(val value: kotlin.Boolean)",
846+
"@JsonProperty(value = \"true\")",
847+
"`true`(true);");
848+
TestUtils.assertFileNotContains(modelKt, "`true`(\"true\")");
849+
}
850+
821851
@Test
822852
public void testJacksonEnumsUseJsonCreator() throws IOException {
823853
File output = Files.createTempDirectory("test").toFile();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Boolean const regression
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
ExceptionState:
9+
type: object
10+
properties:
11+
exceptionPeriodIsClosed:
12+
type: boolean
13+
const: true

0 commit comments

Comments
 (0)