Skip to content

Commit f861dff

Browse files
fix(kotlin): redefined vars should be marked as inherited so that override modifier is added on children class. closes OpenAPITools#22216 (OpenAPITools#22219)
1 parent b99101f commit f861dff

3 files changed

Lines changed: 88 additions & 0 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,28 @@ public CodegenModel fromModel(String name, Schema schema) {
10461046
.collect(Collectors.toMap(CodegenProperty::getBaseName, Function.identity()));
10471047
allVarsMap.keySet()
10481048
.removeAll(m.vars.stream().map(CodegenProperty::getBaseName).collect(Collectors.toSet()));
1049+
1050+
// if there is a parent, find the redefined vars
1051+
if (m.parent != null && m.parentSchema != null) {
1052+
1053+
// get the parent schema
1054+
Schema<?> parentSchema = ModelUtils.getSchemas(this.openAPI).get(m.parentSchema);
1055+
1056+
// if parent schema has properties, find the intersection
1057+
if (parentSchema != null && parentSchema.getProperties() != null) {
1058+
Set<String> varNames = parentSchema.getProperties().keySet();
1059+
1060+
// compute intersection of m.allVars and parent properties, this will give us the overridden properties
1061+
Map<String, CodegenProperty> overriddenProperties = m.allVars.stream()
1062+
.filter(p -> varNames.contains(p.getBaseName()))
1063+
.collect(Collectors.toMap(CodegenProperty::getBaseName, Function.identity()));
1064+
1065+
// overridden properties contain the properties that are redefined in the child model.
1066+
// add them to allVarsMap so that they are marked as inherited.
1067+
allVarsMap.putAll(overriddenProperties);
1068+
}
1069+
}
1070+
10491071
// Update the allVars
10501072
allVarsMap.values().forEach(p -> p.isInherited = true);
10511073
// Update any other vars (requiredVars, optionalVars)

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,32 @@ private void givenSchemaObjectPropertyNameContainsDollarSignWhenGenerateThenDoll
575575
Assert.assertEquals(customKotlinParseListener.getStringReferenceCount(), 0);
576576
}
577577

578+
@Test(description = "add override on reference specialisation")
579+
public void polymorphicReferenceOverrides() throws IOException {
580+
File output = Files.createTempDirectory("test").toFile();
581+
output.deleteOnExit();
582+
// File output = Paths.get("/Users/sylvain_maillard/workspaces/openapi-generator/modules/openapi-generator/target/test").toFile();
583+
584+
final CodegenConfigurator configurator = new CodegenConfigurator()
585+
.setGeneratorName("kotlin")
586+
.setInputSpec("src/test/resources/3_1/issue_22216.yaml")
587+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
588+
589+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
590+
DefaultGenerator generator = new DefaultGenerator();
591+
List<File> files = generator.opts(clientOptInput).generate();
592+
593+
Assert.assertEquals(files.size(), 36);
594+
595+
final Path carFile = Paths.get(output + "/src/main/kotlin/org/openapitools/client/models/Car.kt");
596+
final Path vehicleFile = Paths.get(output + "/src/main/kotlin/org/openapitools/client/models/Vehicle.kt");
597+
// file should contain override keyword for inherited properties ref
598+
TestUtils.assertFileContains(carFile, "override val requiredProperty: kotlin.String,");
599+
TestUtils.assertFileContains(carFile, "override val optionalProperty: kotlin.String? = null");
600+
// file should not contain override keyword for own properties
601+
TestUtils.assertFileNotContains(carFile, "override val color: kotlin.String? = null");
602+
}
603+
578604
@Test(description = "generate polymorphic kotlinx_serialization model")
579605
public void polymorphicKotlinxSerialization() throws IOException {
580606
File output = Files.createTempDirectory("test").toFile();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
openapi: 3.1.0
2+
3+
info:
4+
title: Title
5+
description: Title
6+
version: 1.0.0
7+
8+
paths:
9+
/example:
10+
get:
11+
responses:
12+
"200":
13+
description: "A successful response"
14+
content:
15+
application/json:
16+
schema:
17+
$ref: '#/components/schemas/Vehicle'
18+
19+
components:
20+
schemas:
21+
Vehicle:
22+
type: object
23+
additionalProperties: false
24+
discriminator: { propertyName: objectType }
25+
required: [ objectType, requiredProperty ]
26+
properties:
27+
objectType: { type: string }
28+
requiredProperty: { type: object }
29+
optionalProperty: { type: object }
30+
31+
Car:
32+
allOf:
33+
- $ref: '#/components/schemas/Vehicle'
34+
- type: object
35+
additionalProperties: false
36+
required: [ objectType, requiredProperty ]
37+
properties:
38+
color: { type: string }
39+
requiredProperty: { type: string }
40+
optionalProperty: { type: string }

0 commit comments

Comments
 (0)