Skip to content

Commit 039de98

Browse files
authored
Fix generation of an empty data class when using kotlinx.serialization (#22350)
* Fix generation of an empty data class * Add a test * Delete polymorphicJacksonSerialization artifacts on exit
1 parent 6892768 commit 039de98

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,12 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
989989
additionalProperties.discriminatorValue = mappedModel.getMappingName();
990990
// Remove the discriminator property from the derived class, it is not needed in the generated code
991991
getAllVarProperties(mappedModel.getModel()).forEach(list -> list.removeIf(prop -> prop.name.equals(discriminator.getPropertyName())));
992+
993+
// If model has no properties after removing discriminator, mark it as empty
994+
// so it generates as a class instead of an empty data class
995+
if (mappedModel.getModel().vars.isEmpty() && !mappedModel.getModel().isEnum && !mappedModel.getModel().isAlias) {
996+
mappedModel.getModel().setHasVars(false);
997+
}
992998
}
993999

9941000
}

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ public void polymorphicKotlinxSerialization() throws IOException {
573573
@Test(description = "generate polymorphic jackson model")
574574
public void polymorphicJacksonSerialization() throws IOException {
575575
File output = Files.createTempDirectory("test").toFile();
576-
// output.deleteOnExit();
576+
output.deleteOnExit();
577577

578578
final CodegenConfigurator configurator = new CodegenConfigurator()
579579
.setGeneratorName("kotlin")
@@ -644,6 +644,36 @@ public void testIntArrayToEnum() throws IOException {
644644
TestUtils.assertFileContains(modelKt, "enum class DaysOfWeek(val value: kotlin.Int)");
645645
}
646646

647+
@Test(description = "convert an empty model to object")
648+
public void emptyModelKotlinxSerializationTest() throws IOException {
649+
final Schema<?> schema = new ObjectSchema()
650+
.description("an empty model");
651+
final DefaultCodegen codegen = new KotlinClientCodegen();
652+
codegen.processOpts();
653+
654+
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("EmptyModel", schema);
655+
codegen.setOpenAPI(openAPI);
656+
657+
File output = Files.createTempDirectory("test").toFile();
658+
output.deleteOnExit();
659+
660+
final CodegenConfigurator configurator = new CodegenConfigurator()
661+
.setGeneratorName("kotlin")
662+
.setAdditionalProperties(new HashMap<>() {{
663+
put(CodegenConstants.MODEL_PACKAGE, "model");
664+
put(SERIALIZATION_LIBRARY, "kotlinx_serialization");
665+
}})
666+
.setInputSpec("src/test/resources/3_0/kotlin/empty-model.yaml")
667+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
668+
669+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
670+
DefaultGenerator generator = new DefaultGenerator();
671+
generator.opts(clientOptInput).generate();
672+
673+
final Path modelKt = Paths.get(output + "/src/main/kotlin/model/EmptyModel.kt");
674+
TestUtils.assertFileNotContains(modelKt, "data class EmptyModel");
675+
}
676+
647677
private static class ModelNameTest {
648678
private final String expectedName;
649679
private final String expectedClassName;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Test Empty Model
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
Response:
9+
type: "object"
10+
properties:
11+
result_or_error:
12+
nullable: true
13+
discriminator:
14+
propertyName: "_type"
15+
mapping:
16+
result: "#/components/schemas/EmptyModel"
17+
error: "#/components/schemas/Error"
18+
oneOf:
19+
- $ref: "#/components/schemas/EmptyModel"
20+
- $ref: "#/components/schemas/Error"
21+
EmptyModel:
22+
type: object
23+
properties:
24+
_type:
25+
required:
26+
- "_type"
27+
type: string
28+
default: "result"
29+
additionalProperties: false
30+
Error:
31+
type: object
32+
required:
33+
- "code"
34+
properties:
35+
code:
36+
type: integer
37+
_type:
38+
required:
39+
- "_type"
40+
type: string
41+
default: "error"
42+
additionalProperties: false

0 commit comments

Comments
 (0)