Skip to content

Commit c53cd6d

Browse files
amakhrovmichaelpro1
authored andcommitted
Ensure model.allParents always includes model.parent. (OpenAPITools#5738)
`allParents` is used by generators with multiple inheritance, e.g typescript and perl
1 parent a387205 commit c53cd6d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,6 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
12361236
public static List<String> getAllParentsName(ComposedSchema composedSchema, Map<String, Schema> allSchemas, boolean includeAncestors) {
12371237
List<Schema> interfaces = getInterfaces(composedSchema);
12381238
List<String> names = new ArrayList<String>();
1239-
List<String> refedWithoutDiscriminator = new ArrayList<>();
12401239

12411240
if (interfaces != null && !interfaces.isEmpty()) {
12421241
for (Schema schema : interfaces) {
@@ -1255,18 +1254,18 @@ public static List<String> getAllParentsName(ComposedSchema composedSchema, Map<
12551254
}
12561255
} else {
12571256
// not a parent since discriminator.propertyName is not set
1258-
refedWithoutDiscriminator.add(parentName);
12591257
}
12601258
} else {
12611259
// not a ref, doing nothing
12621260
}
12631261
}
12641262
}
12651263

1266-
if (names.size() == 0 && refedWithoutDiscriminator.size() == 1) {
1267-
LOGGER.warn("[deprecated] inheritance without use of 'discriminator.propertyName' is deprecated " +
1268-
"and will be removed in a future release. Generating model for {}. Title: {}", composedSchema.getName(), composedSchema.getTitle());
1269-
return refedWithoutDiscriminator;
1264+
// ensure `allParents` always includes `parent`
1265+
// this is more robust than keeping logic in getParentName() and getAllParentsName() in sync
1266+
String parentName = getParentName(composedSchema, allSchemas);
1267+
if (parentName != null && !names.contains(parentName)) {
1268+
names.add(parentName);
12701269
}
12711270

12721271
return names;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,18 @@ public void testAllOfRequired() {
604604
Assert.assertEquals(getRequiredVars(childModel), Collections.singletonList("name"));
605605
}
606606

607+
@Test
608+
public void testAllOfSingleRefWithOwnPropsNoDiscriminator() {
609+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/composed-allof.yaml");
610+
final DefaultCodegen codegen = new CodegenWithMultipleInheritance();
611+
612+
Schema schema = openAPI.getComponents().getSchemas().get("MessageEventCoreWithTimeListEntries");
613+
codegen.setOpenAPI(openAPI);
614+
CodegenModel model = codegen.fromModel("MessageEventCoreWithTimeListEntries", schema);
615+
Assert.assertEquals(model.parent, "MessageEventCore");
616+
Assert.assertEquals(model.allParents, Collections.singletonList("MessageEventCore"));
617+
}
618+
607619
@Test
608620
public void testAllOfSingleRefNoOwnProps() {
609621
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/composed-allof.yaml");

0 commit comments

Comments
 (0)