Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,28 @@ public CodegenModel fromModel(String name, Schema schema) {
.collect(Collectors.toMap(CodegenProperty::getBaseName, Function.identity()));
allVarsMap.keySet()
.removeAll(m.vars.stream().map(CodegenProperty::getBaseName).collect(Collectors.toSet()));

// if there is a parent, find the redefined vars
if (m.parent != null && m.parentSchema != null) {

// get the parent schema
Schema<?> parentSchema = ModelUtils.getSchemas(this.openAPI).get(m.parentSchema);

// if parent schema has properties, find the intersection
if (parentSchema != null && parentSchema.getProperties() != null) {
Set<String> varNames = parentSchema.getProperties().keySet();

// compute intersection of m.allVars and parent properties, this will give us the overridden properties
Map<String, CodegenProperty> overriddenProperties = m.allVars.stream()
.filter(p -> varNames.contains(p.getBaseName()))
.collect(Collectors.toMap(CodegenProperty::getBaseName, Function.identity()));

// overridden properties contain the properties that are redefined in the child model.
// add them to allVarsMap so that they are marked as inherited.
allVarsMap.putAll(overriddenProperties);
}
}

// Update the allVars
allVarsMap.values().forEach(p -> p.isInherited = true);
// Update any other vars (requiredVars, optionalVars)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;

import org.junit.jupiter.api.Disabled;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand All @@ -29,7 +28,7 @@ public void setUp() {
System.getProperties().putAll(props);
}

@Test @Disabled
@Test(enabled = false)
// comment out the following tests as it generates false alarms from time to time
// also using system property will eventually be decommissioned
public void testNonStringSystemProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,32 @@ private void givenSchemaObjectPropertyNameContainsDollarSignWhenGenerateThenDoll
Assert.assertEquals(customKotlinParseListener.getStringReferenceCount(), 0);
}

@Test(description = "add override on reference specialisation")
public void polymorphicReferenceOverrides() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();
// File output = Paths.get("/Users/sylvain_maillard/workspaces/openapi-generator/modules/openapi-generator/target/test").toFile();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("kotlin")
.setInputSpec("src/test/resources/3_1/issue_22216.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();

Assert.assertEquals(files.size(), 36);

final Path carFile = Paths.get(output + "/src/main/kotlin/org/openapitools/client/models/Car.kt");
final Path vehicleFile = Paths.get(output + "/src/main/kotlin/org/openapitools/client/models/Vehicle.kt");
// file should contain override keyword for inherited properties ref
TestUtils.assertFileContains(carFile, "override val requiredProperty: kotlin.String,");
TestUtils.assertFileContains(carFile, "override val optionalProperty: kotlin.String? = null");
// file should not contain override keyword for own properties
TestUtils.assertFileNotContains(carFile, "override val color: kotlin.String? = null");
}

@Test(description = "generate polymorphic kotlinx_serialization model")
public void polymorphicKotlinxSerialization() throws IOException {
File output = Files.createTempDirectory("test").toFile();
Expand Down
40 changes: 40 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/issue_22216.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
openapi: 3.1.0

info:
title: Title
description: Title
version: 1.0.0

paths:
/example:
get:
responses:
"200":
description: "A successful response"
content:
application/json:
schema:
$ref: '#/components/schemas/Vehicle'

components:
schemas:
Vehicle:
type: object
additionalProperties: false
discriminator: { propertyName: objectType }
required: [ objectType, requiredProperty ]
properties:
objectType: { type: string }
requiredProperty: { type: object }
optionalProperty: { type: object }

Car:
allOf:
- $ref: '#/components/schemas/Vehicle'
- type: object
additionalProperties: false
required: [ objectType, requiredProperty ]
properties:
color: { type: string }
requiredProperty: { type: string }
optionalProperty: { type: string }