Skip to content

Commit

Permalink
Ensure model.allParents always includes model.parent. (OpenAPIToo…
Browse files Browse the repository at this point in the history
…ls#5738)

`allParents` is used by generators with multiple inheritance, e.g typescript and perl
  • Loading branch information
amakhrov authored and MikailBag committed May 31, 2020
1 parent 14c6045 commit 39ac60f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,6 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
public static List<String> getAllParentsName(ComposedSchema composedSchema, Map<String, Schema> allSchemas, boolean includeAncestors) {
List<Schema> interfaces = getInterfaces(composedSchema);
List<String> names = new ArrayList<String>();
List<String> refedWithoutDiscriminator = new ArrayList<>();

if (interfaces != null && !interfaces.isEmpty()) {
for (Schema schema : interfaces) {
Expand All @@ -1255,18 +1254,18 @@ public static List<String> getAllParentsName(ComposedSchema composedSchema, Map<
}
} else {
// not a parent since discriminator.propertyName is not set
refedWithoutDiscriminator.add(parentName);
}
} else {
// not a ref, doing nothing
}
}
}

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

return names;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,18 @@ public void testAllOfRequired() {
Assert.assertEquals(getRequiredVars(childModel), Collections.singletonList("name"));
}

@Test
public void testAllOfSingleRefWithOwnPropsNoDiscriminator() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/composed-allof.yaml");
final DefaultCodegen codegen = new CodegenWithMultipleInheritance();

Schema schema = openAPI.getComponents().getSchemas().get("MessageEventCoreWithTimeListEntries");
codegen.setOpenAPI(openAPI);
CodegenModel model = codegen.fromModel("MessageEventCoreWithTimeListEntries", schema);
Assert.assertEquals(model.parent, "MessageEventCore");
Assert.assertEquals(model.allParents, Collections.singletonList("MessageEventCore"));
}

@Test
public void testAllOfSingleRefNoOwnProps() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/composed-allof.yaml");
Expand Down

0 comments on commit 39ac60f

Please sign in to comment.