Skip to content

Commit 8d148d7

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

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

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

577+
@Test(description = "add override on reference specialisation")
578+
public void polymorphicReferenceOverrides() throws IOException {
579+
File output = Files.createTempDirectory("test").toFile();
580+
output.deleteOnExit();
581+
// File output = Paths.get("/Users/sylvain_maillard/workspaces/openapi-generator/modules/openapi-generator/target/test").toFile();
582+
583+
final CodegenConfigurator configurator = new CodegenConfigurator()
584+
.setGeneratorName("kotlin")
585+
.setInputSpec("src/test/resources/3_1/issue_22216.yaml")
586+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
587+
588+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
589+
DefaultGenerator generator = new DefaultGenerator();
590+
List<File> files = generator.opts(clientOptInput).generate();
591+
592+
Assert.assertEquals(files.size(), 36);
593+
594+
final Path carFile = Paths.get(output + "/src/main/kotlin/org/openapitools/client/models/Car.kt");
595+
final Path vehicleFile = Paths.get(output + "/src/main/kotlin/org/openapitools/client/models/Vehicle.kt");
596+
// file should contain override keyword for inherited properties ref
597+
TestUtils.assertFileContains(carFile, "override val requiredProperty: kotlin.String,");
598+
TestUtils.assertFileContains(carFile, "override val optionalProperty: kotlin.String? = null");
599+
// file should not contain override keyword for own properties
600+
TestUtils.assertFileNotContains(carFile, "override val color: kotlin.String? = null");
601+
}
602+
577603
@Test(description = "generate polymorphic kotlinx_serialization model")
578604
public void polymorphicKotlinxSerialization() throws IOException {
579605
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)