Skip to content

Commit

Permalink
Correct allOf with only one child schema (no discriminator) (#7855)
Browse files Browse the repository at this point in the history
* Correct allOf with only one child schema (no discriminator

* fix tests
  • Loading branch information
wing328 authored Nov 23, 2020
1 parent 14ff8e0 commit 4984f9c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1316,14 +1316,12 @@ public static String getParentName(ComposedSchema composedSchema, Map<String, Sc
}
}

// parent name only makes sense when there is a single obvious parent
if (refedWithoutDiscriminator.size() == 1) {
if (hasAmbiguousParents) {
LOGGER.warn("[deprecated] inheritance without use of 'discriminator.propertyName' is deprecated " +
"and will be removed in a future release. Generating model for composed schema name: {}. Title: {}",
composedSchema.getName(), composedSchema.getTitle());
}
return refedWithoutDiscriminator.get(0);
if (refedWithoutDiscriminator.size() == 1 && hasAmbiguousParents) {
// allOf with a single $ref (no discriminator)
// TODO to be removed in 5.x or 6.x release
LOGGER.info("[deprecated] inheritance without use of 'discriminator.propertyName' has been deprecated" +
" in the 5.x release. Composed schema name: {}. Title: {}",
composedSchema.getName(), composedSchema.getTitle());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1387,9 +1387,9 @@ public void testAllOfSingleRefNoOwnProps() {
Schema schema = openAPI.getComponents().getSchemas().get("NewMessageEventCoreNoOwnProps");
codegen.setOpenAPI(openAPI);
CodegenModel model = codegen.fromModel("NewMessageEventCoreNoOwnProps", schema);
Assert.assertEquals(getNames(model.getVars()), Collections.emptyList());
Assert.assertEquals(model.parent, "MessageEventCore");
Assert.assertEquals(model.allParents, Collections.singletonList("MessageEventCore"));
Assert.assertEquals(getNames(model.getVars()), Arrays.asList("id","message"));
Assert.assertNull(model.parent);
Assert.assertNull(model.allParents);
}

class CodegenWithMultipleInheritance extends DefaultCodegen {
Expand All @@ -1400,7 +1400,6 @@ public CodegenWithMultipleInheritance() {
}
}


@Test
public void testAllOfParent() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/allOf-required-parent.yaml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,10 +957,10 @@ public void testAllowModelWithNoProperties() throws Exception {
validateJavaSourceFiles(files);

TestUtils.assertFileContains(Paths.get(output + "/src/main/java/org/openapitools/client/model/RealCommand.java"),
"class RealCommand extends Command");
"class RealCommand {");

TestUtils.assertFileContains(Paths.get(output + "/src/main/java/org/openapitools/client/model/Command.java"),
"class Command");
"class Command {");

output.deleteOnExit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
Expand Down Expand Up @@ -57,8 +58,8 @@ public void javaInheritanceTest() {

Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.parent, "Base");
Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
Assert.assertNull(cm.parent);
Assert.assertEquals(cm.imports, Collections.emptySet());
}

@Test(description = "convert a composed model with discriminator")
Expand Down

0 comments on commit 4984f9c

Please sign in to comment.