Skip to content

Commit 1338243

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

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
@@ -855,6 +855,28 @@ public CodegenModel fromModel(String name, Schema schema) {
855855
.collect(Collectors.toMap(CodegenProperty::getBaseName, Function.identity()));
856856
allVarsMap.keySet()
857857
.removeAll(m.vars.stream().map(CodegenProperty::getBaseName).collect(Collectors.toSet()));
858+
859+
// if there is a parent, find the redefined vars
860+
if (m.parent != null && m.parentSchema != null) {
861+
862+
// get the parent schema
863+
Schema<?> parentSchema = ModelUtils.getSchemas(this.openAPI).get(m.parentSchema);
864+
865+
// if parent schema has properties, find the intersection
866+
if (parentSchema != null && parentSchema.getProperties() != null) {
867+
Set<String> varNames = parentSchema.getProperties().keySet();
868+
869+
// compute intersection of m.allVars and parent properties, this will give us the overridden properties
870+
Map<String, CodegenProperty> overriddenProperties = m.allVars.stream()
871+
.filter(p -> varNames.contains(p.getBaseName()))
872+
.collect(Collectors.toMap(CodegenProperty::getBaseName, Function.identity()));
873+
874+
// overridden properties contain the properties that are redefined in the child model.
875+
// add them to allVarsMap so that they are marked as inherited.
876+
allVarsMap.putAll(overriddenProperties);
877+
}
878+
}
879+
858880
// Update the allVars
859881
allVarsMap.values().forEach(p -> p.isInherited = true);
860882
// 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
@@ -529,6 +529,32 @@ private void givenSchemaObjectPropertyNameContainsDollarSignWhenGenerateThenDoll
529529
Assert.assertEquals(customKotlinParseListener.getStringReferenceCount(), 0);
530530
}
531531

532+
@Test(description = "add override on reference specialisation")
533+
public void polymorphicReferenceOverrides() throws IOException {
534+
File output = Files.createTempDirectory("test").toFile();
535+
output.deleteOnExit();
536+
// File output = Paths.get("/Users/sylvain_maillard/workspaces/openapi-generator/modules/openapi-generator/target/test").toFile();
537+
538+
final CodegenConfigurator configurator = new CodegenConfigurator()
539+
.setGeneratorName("kotlin")
540+
.setInputSpec("src/test/resources/3_1/issue_22216.yaml")
541+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
542+
543+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
544+
DefaultGenerator generator = new DefaultGenerator();
545+
List<File> files = generator.opts(clientOptInput).generate();
546+
547+
Assert.assertEquals(files.size(), 36);
548+
549+
final Path carFile = Paths.get(output + "/src/main/kotlin/org/openapitools/client/models/Car.kt");
550+
final Path vehicleFile = Paths.get(output + "/src/main/kotlin/org/openapitools/client/models/Vehicle.kt");
551+
// file should contain override keyword for inherited properties ref
552+
TestUtils.assertFileContains(carFile, "override val requiredProperty: kotlin.String,");
553+
TestUtils.assertFileContains(carFile, "override val optionalProperty: kotlin.String? = null");
554+
// file should not contain override keyword for own properties
555+
TestUtils.assertFileNotContains(carFile, "override val color: kotlin.String? = null");
556+
}
557+
532558
@Test(description = "generate polymorphic kotlinx_serialization model")
533559
public void polymorphicKotlinxSerialization() throws IOException {
534560
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)