Skip to content

Commit ddabfdc

Browse files
committed
fix ModelUtils.getParentName returning name of first element in composed schema instead of null when there are multiple elements and it is not clear which one should be parent
1 parent 4ab8850 commit ddabfdc

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,17 +1573,18 @@ public static String getParentName(Schema composedSchema, Map<String, Schema> al
15731573
List<String> refedWithoutDiscriminator = new ArrayList<>();
15741574

15751575
if (interfaces != null && !interfaces.isEmpty()) {
1576+
List<String> parentNameCandidates = new ArrayList<>(interfaces.size());
15761577
for (Schema schema : interfaces) {
15771578
// get the actual schema
15781579
if (StringUtils.isNotEmpty(schema.get$ref())) {
15791580
String parentName = getSimpleRef(schema.get$ref());
15801581
Schema s = allSchemas.get(parentName);
15811582
if (s == null) {
15821583
LOGGER.error("Failed to obtain schema from {}", parentName);
1583-
return "UNKNOWN_PARENT_NAME";
1584+
parentNameCandidates.add("UNKNOWN_PARENT_NAME");
15841585
} else if (hasOrInheritsDiscriminator(s, allSchemas, new ArrayList<Schema>())) {
15851586
// discriminator.propertyName is used or x-parent is used
1586-
return parentName;
1587+
parentNameCandidates.add(parentName);
15871588
} else {
15881589
// not a parent since discriminator.propertyName or x-parent is not set
15891590
hasAmbiguousParents = true;
@@ -1600,6 +1601,12 @@ public static String getParentName(Schema composedSchema, Map<String, Schema> al
16001601
}
16011602
}
16021603
}
1604+
if (parentNameCandidates.size() > 1) {
1605+
// unclear which one should be the parent
1606+
return null;
1607+
} else if (parentNameCandidates.size() == 1) {
1608+
return parentNameCandidates.get(0);
1609+
}
16031610
if (refedWithoutDiscriminator.size() == 1 && nullSchemaChildrenCount == 1) {
16041611
// One schema is a $ref and the other is the 'null' type, so the parent is obvious.
16051612
// In this particular case there is no need to specify a discriminator.

modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,4 +675,12 @@ public void testModelWithPropertiesOnly() {
675675
testSchema.setAdditionalProperties(new Schema().type("string"));
676676
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
677677
}
678+
679+
@Test
680+
public void getParentNameMultipleInterfacesTest() {
681+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf_inheritance.yaml");
682+
Map<String, Schema> allSchemas = openAPI.getComponents().getSchemas();
683+
Schema composedSchema = allSchemas.get("RandomAnimalsResponse_animals_inner");
684+
assertNull(ModelUtils.getParentName(composedSchema, allSchemas));
685+
}
678686
}

0 commit comments

Comments
 (0)