Skip to content

Commit 5493412

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 7298912 commit 5493412

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

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
@@ -1565,17 +1565,18 @@ public static String getParentName(Schema composedSchema, Map<String, Schema> al
15651565
List<String> refedWithoutDiscriminator = new ArrayList<>();
15661566

15671567
if (interfaces != null && !interfaces.isEmpty()) {
1568+
List<String> parentNameCandidates = new ArrayList<>(interfaces.size());
15681569
for (Schema schema : interfaces) {
15691570
// get the actual schema
15701571
if (StringUtils.isNotEmpty(schema.get$ref())) {
15711572
String parentName = getSimpleRef(schema.get$ref());
15721573
Schema s = allSchemas.get(parentName);
15731574
if (s == null) {
15741575
LOGGER.error("Failed to obtain schema from {}", parentName);
1575-
return "UNKNOWN_PARENT_NAME";
1576+
parentNameCandidates.add("UNKNOWN_PARENT_NAME");
15761577
} else if (hasOrInheritsDiscriminator(s, allSchemas, new ArrayList<Schema>())) {
15771578
// discriminator.propertyName is used or x-parent is used
1578-
return parentName;
1579+
parentNameCandidates.add(parentName);
15791580
} else {
15801581
// not a parent since discriminator.propertyName or x-parent is not set
15811582
hasAmbiguousParents = true;
@@ -1592,6 +1593,12 @@ public static String getParentName(Schema composedSchema, Map<String, Schema> al
15921593
}
15931594
}
15941595
}
1596+
if (parentNameCandidates.size() > 1) {
1597+
// unclear which one should be the parent
1598+
return null;
1599+
} else if (parentNameCandidates.size() == 1) {
1600+
return parentNameCandidates.get(0);
1601+
}
15951602
if (refedWithoutDiscriminator.size() == 1 && nullSchemaChildrenCount == 1) {
15961603
// One schema is a $ref and the other is the 'null' type, so the parent is obvious.
15971604
// 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
@@ -641,4 +641,12 @@ public void isUnsupportedSchemaTest() {
641641
assertFalse(ModelUtils.isUnsupportedSchema(openAPI, (Schema) property2.getProperties().get("condition")));
642642
assertFalse(ModelUtils.isUnsupportedSchema(openAPI, (Schema) property2.getProperties().get("purpose")));
643643
}
644+
645+
@Test
646+
public void getParentNameMultipleInterfacesTest() {
647+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf_inheritance.yaml");
648+
Map<String, Schema> allSchemas = openAPI.getComponents().getSchemas();
649+
Schema composedSchema = allSchemas.get("RandomAnimalsResponse_animals_inner");
650+
assertNull(ModelUtils.getParentName(composedSchema, allSchemas));
651+
}
644652
}

0 commit comments

Comments
 (0)