Ensure model.allParents always includes model.parent. (#5738)

`allParents` is used by generators with multiple inheritance, e.g typescript and perl
This commit is contained in:
Alexey Makhrov 2020-04-18 00:22:20 -07:00 committed by GitHub
parent e47739dda5
commit c5472be422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions

View File

@ -1236,7 +1236,6 @@ public class ModelUtils {
public static List<String> getAllParentsName(ComposedSchema composedSchema, Map<String, Schema> allSchemas, boolean includeAncestors) { public static List<String> getAllParentsName(ComposedSchema composedSchema, Map<String, Schema> allSchemas, boolean includeAncestors) {
List<Schema> interfaces = getInterfaces(composedSchema); List<Schema> interfaces = getInterfaces(composedSchema);
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
List<String> refedWithoutDiscriminator = new ArrayList<>();
if (interfaces != null && !interfaces.isEmpty()) { if (interfaces != null && !interfaces.isEmpty()) {
for (Schema schema : interfaces) { for (Schema schema : interfaces) {
@ -1255,7 +1254,6 @@ public class ModelUtils {
} }
} else { } else {
// not a parent since discriminator.propertyName is not set // not a parent since discriminator.propertyName is not set
refedWithoutDiscriminator.add(parentName);
} }
} else { } else {
// not a ref, doing nothing // not a ref, doing nothing
@ -1263,10 +1261,11 @@ public class ModelUtils {
} }
} }
if (names.size() == 0 && refedWithoutDiscriminator.size() == 1) { // ensure `allParents` always includes `parent`
LOGGER.warn("[deprecated] inheritance without use of 'discriminator.propertyName' is deprecated " + // this is more robust than keeping logic in getParentName() and getAllParentsName() in sync
"and will be removed in a future release. Generating model for {}. Title: {}", composedSchema.getName(), composedSchema.getTitle()); String parentName = getParentName(composedSchema, allSchemas);
return refedWithoutDiscriminator; if (parentName != null && !names.contains(parentName)) {
names.add(parentName);
} }
return names; return names;

View File

@ -604,6 +604,18 @@ public class DefaultCodegenTest {
Assert.assertEquals(getRequiredVars(childModel), Collections.singletonList("name")); 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 @Test
public void testAllOfSingleRefNoOwnProps() { public void testAllOfSingleRefNoOwnProps() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/composed-allof.yaml"); final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/composed-allof.yaml");