forked from loafle/openapi-generator-original
set interface with discriminator as parent
If a composed model (allOf) doesn't have any parent and one of its interface has a discriminator field, then set this interface as parent. See #2096 See https://github.com/swagger-api/swagger-parser/pull/246
This commit is contained in:
parent
bd0340327b
commit
8740b4d0ea
@ -1195,7 +1195,35 @@ public class DefaultCodegen {
|
||||
allRequired = null;
|
||||
}
|
||||
// parent model
|
||||
final RefModel parent = (RefModel) composed.getParent();
|
||||
RefModel parent = (RefModel) composed.getParent();
|
||||
|
||||
// interfaces (intermediate models)
|
||||
if (composed.getInterfaces() != null) {
|
||||
if (m.interfaces == null)
|
||||
m.interfaces = new ArrayList<String>();
|
||||
for (RefModel _interface : composed.getInterfaces()) {
|
||||
Model interfaceModel = null;
|
||||
if (allDefinitions != null) {
|
||||
interfaceModel = allDefinitions.get(_interface.getSimpleRef());
|
||||
}
|
||||
// set first interface with discriminator found as parent
|
||||
if (parent == null && interfaceModel instanceof ModelImpl && ((ModelImpl) interfaceModel).getDiscriminator() != null) {
|
||||
parent = _interface;
|
||||
} else {
|
||||
final String interfaceRef = toModelName(_interface.getSimpleRef());
|
||||
m.interfaces.add(interfaceRef);
|
||||
addImport(m, interfaceRef);
|
||||
if (allDefinitions != null) {
|
||||
if (supportsInheritance) {
|
||||
addProperties(allProperties, allRequired, interfaceModel, allDefinitions);
|
||||
} else {
|
||||
addProperties(properties, required, interfaceModel, allDefinitions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (parent != null) {
|
||||
final String parentRef = parent.getSimpleRef();
|
||||
m.parentSchema = parentRef;
|
||||
@ -1210,24 +1238,7 @@ public class DefaultCodegen {
|
||||
}
|
||||
}
|
||||
}
|
||||
// interfaces (intermediate models)
|
||||
if (composed.getInterfaces() != null) {
|
||||
if (m.interfaces == null)
|
||||
m.interfaces = new ArrayList<String>();
|
||||
for (RefModel _interface : composed.getInterfaces()) {
|
||||
final String interfaceRef = toModelName(_interface.getSimpleRef());
|
||||
m.interfaces.add(interfaceRef);
|
||||
addImport(m, interfaceRef);
|
||||
if (allDefinitions != null) {
|
||||
final Model interfaceModel = allDefinitions.get(_interface.getSimpleRef());
|
||||
if (supportsInheritance) {
|
||||
addProperties(allProperties, allRequired, interfaceModel, allDefinitions);
|
||||
} else {
|
||||
addProperties(properties, required, interfaceModel, allDefinitions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// child model (properties owned by the model itself)
|
||||
Model child = composed.getChild();
|
||||
if (child != null && child instanceof RefModel && allDefinitions != null) {
|
||||
|
@ -13,10 +13,14 @@ import com.google.common.collect.Sets;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JavaInheritanceTest {
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
@Test(description = "convert a composed model")
|
||||
@Test(description = "convert a composed model with parent")
|
||||
public void javaInheritanceTest() {
|
||||
final Model model = new ComposedModel().parent(new RefModel("Base"))
|
||||
.child(new ModelImpl().additionalProperties(new StringProperty()));
|
||||
@ -29,4 +33,26 @@ public class JavaInheritanceTest {
|
||||
Assert.assertEquals(cm.parent, "Base");
|
||||
Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-method")
|
||||
@Test(description = "convert a composed model with discriminator")
|
||||
public void javaInheritanceWithDiscriminatorTest() {
|
||||
ModelImpl base = new ModelImpl();
|
||||
base.setDiscriminator("disc");
|
||||
|
||||
final Model model = new ComposedModel()
|
||||
.interfaces(Arrays.asList(new RefModel("Base")))
|
||||
.child(new ModelImpl().additionalProperties(new StringProperty()));
|
||||
|
||||
final Map<String, Model> allDefinitions = new HashMap<String, Model>();
|
||||
allDefinitions.put("Base", base);
|
||||
|
||||
final DefaultCodegen codegen = new JavaClientCodegen();
|
||||
final CodegenModel cm = codegen.fromModel("sample", model, allDefinitions);
|
||||
|
||||
Assert.assertEquals(cm.name, "sample");
|
||||
Assert.assertEquals(cm.classname, "Sample");
|
||||
Assert.assertEquals(cm.parent, "Base");
|
||||
Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class JavaScriptInheritanceTest {
|
||||
Assert.assertEquals(cm.vars.size(), 1);
|
||||
Assert.assertEquals(cm.vars.get(0).name, "childProp");
|
||||
Assert.assertEquals(cm.allVars.size(), 4);
|
||||
String[] allVars = {"baseProp", "intf1Prop", "intf2Prop", "childProp"};
|
||||
String[] allVars = {"intf1Prop", "intf2Prop", "baseProp", "childProp"};
|
||||
for (int i = 0; i < allVars.length; i++) {
|
||||
Assert.assertEquals(cm.allVars.get(i).name, allVars[i]);
|
||||
}
|
||||
@ -91,7 +91,7 @@ public class JavaScriptInheritanceTest {
|
||||
Assert.assertEquals(cm.imports, Sets.newHashSet("Base", "Interface1", "Interface2"));
|
||||
Assert.assertEquals(cm.vars.size(), 4);
|
||||
Assert.assertEquals(cm.allVars.size(), 4);
|
||||
String[] allVars = {"baseProp", "intf1Prop", "intf2Prop", "childProp"};
|
||||
String[] allVars = {"intf1Prop", "intf2Prop", "baseProp", "childProp"};
|
||||
for (int i = 0; i < allVars.length; i++) {
|
||||
Assert.assertEquals(cm.vars.get(i).name, allVars[i]);
|
||||
Assert.assertEquals(cm.allVars.get(i).name, allVars[i]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user