Compare commits

...

1 Commits

Author SHA1 Message Date
William Cheng
75f9335515 fix single allof resulting in inheritance 2020-07-09 17:30:28 +08:00
3 changed files with 10 additions and 25 deletions

View File

@ -1222,8 +1222,6 @@ public class ModelUtils {
public static String getParentName(ComposedSchema composedSchema, Map<String, Schema> allSchemas) { public static String getParentName(ComposedSchema composedSchema, Map<String, Schema> allSchemas) {
List<Schema> interfaces = getInterfaces(composedSchema); List<Schema> interfaces = getInterfaces(composedSchema);
int nullSchemaChildrenCount = 0; int nullSchemaChildrenCount = 0;
boolean hasAmbiguousParents = false;
List<String> refedWithoutDiscriminator = new ArrayList<>();
if (interfaces != null && !interfaces.isEmpty()) { if (interfaces != null && !interfaces.isEmpty()) {
for (Schema schema : interfaces) { for (Schema schema : interfaces) {
@ -1238,9 +1236,10 @@ public class ModelUtils {
// discriminator.propertyName is used // discriminator.propertyName is used
return parentName; return parentName;
} else { } else {
// not a parent since discriminator.propertyName is not set // TOOD to be removed in 6.x release
hasAmbiguousParents = true; LOGGER.warn("[deprecated] inheritance without use of 'discriminator.propertyName' has been deprecated" +
refedWithoutDiscriminator.add(parentName); " in the 5.x release. Composed schema name: {}. Title: {}",
composedSchema.getName(), composedSchema.getTitle());
} }
} else { } else {
// not a ref, doing nothing, except counting the number of times the 'null' type // not a ref, doing nothing, except counting the number of times the 'null' type
@ -1253,21 +1252,6 @@ public class ModelUtils {
} }
} }
} }
if (refedWithoutDiscriminator.size() == 1 && nullSchemaChildrenCount == 1) {
// One schema is a $ref and the other is the 'null' type, so the parent is obvious.
// In this particular case there is no need to specify a discriminator.
hasAmbiguousParents = false;
}
}
// 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);
} }
return null; return null;

View File

@ -1372,9 +1372,9 @@ public class DefaultCodegenTest {
Schema schema = openAPI.getComponents().getSchemas().get("NewMessageEventCoreNoOwnProps"); Schema schema = openAPI.getComponents().getSchemas().get("NewMessageEventCoreNoOwnProps");
codegen.setOpenAPI(openAPI); codegen.setOpenAPI(openAPI);
CodegenModel model = codegen.fromModel("NewMessageEventCoreNoOwnProps", schema); CodegenModel model = codegen.fromModel("NewMessageEventCoreNoOwnProps", schema);
Assert.assertEquals(getNames(model.getVars()), Collections.emptyList()); Assert.assertEquals(getNames(model.getVars()), Arrays.asList("id","message"));
Assert.assertEquals(model.parent, "MessageEventCore"); Assert.assertNull(model.parent);
Assert.assertEquals(model.allParents, Collections.singletonList("MessageEventCore")); Assert.assertNull(model.allParents);
} }
class CodegenWithMultipleInheritance extends DefaultCodegen { class CodegenWithMultipleInheritance extends DefaultCodegen {

View File

@ -25,6 +25,7 @@ import io.swagger.v3.oas.models.media.Discriminator;
import io.swagger.v3.oas.models.media.ObjectSchema; import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema; import io.swagger.v3.oas.models.media.StringSchema;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.CodegenProperty;
@ -57,8 +58,8 @@ public class JavaInheritanceTest {
Assert.assertEquals(cm.name, "sample"); Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "Sample"); Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.parent, "Base"); Assert.assertNull(cm.parent);
Assert.assertEquals(cm.imports, Sets.newHashSet("Base")); Assert.assertEquals(cm.imports, Collections.emptySet());
} }
@Test(description = "convert a composed model with discriminator") @Test(description = "convert a composed model with discriminator")