Skip to content

Commit 2ce9a11

Browse files
committed
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 swagger-api#2096 See swagger-api/swagger-parser#246
1 parent 89741c7 commit 2ce9a11

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,35 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
11951195
allRequired = null;
11961196
}
11971197
// parent model
1198-
final RefModel parent = (RefModel) composed.getParent();
1198+
RefModel parent = (RefModel) composed.getParent();
1199+
1200+
// interfaces (intermediate models)
1201+
if (composed.getInterfaces() != null) {
1202+
if (m.interfaces == null)
1203+
m.interfaces = new ArrayList<String>();
1204+
for (RefModel _interface : composed.getInterfaces()) {
1205+
Model interfaceModel = null;
1206+
if (allDefinitions != null) {
1207+
interfaceModel = allDefinitions.get(_interface.getSimpleRef());
1208+
}
1209+
// set first interface with discriminator found as parent
1210+
if (parent == null && interfaceModel instanceof ModelImpl && ((ModelImpl) interfaceModel).getDiscriminator() != null) {
1211+
parent = _interface;
1212+
} else {
1213+
final String interfaceRef = toModelName(_interface.getSimpleRef());
1214+
m.interfaces.add(interfaceRef);
1215+
addImport(m, interfaceRef);
1216+
if (allDefinitions != null) {
1217+
if (supportsInheritance) {
1218+
addProperties(allProperties, allRequired, interfaceModel, allDefinitions);
1219+
} else {
1220+
addProperties(properties, required, interfaceModel, allDefinitions);
1221+
}
1222+
}
1223+
}
1224+
}
1225+
}
1226+
11991227
if (parent != null) {
12001228
final String parentRef = parent.getSimpleRef();
12011229
m.parentSchema = parentRef;
@@ -1210,24 +1238,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
12101238
}
12111239
}
12121240
}
1213-
// interfaces (intermediate models)
1214-
if (composed.getInterfaces() != null) {
1215-
if (m.interfaces == null)
1216-
m.interfaces = new ArrayList<String>();
1217-
for (RefModel _interface : composed.getInterfaces()) {
1218-
final String interfaceRef = toModelName(_interface.getSimpleRef());
1219-
m.interfaces.add(interfaceRef);
1220-
addImport(m, interfaceRef);
1221-
if (allDefinitions != null) {
1222-
final Model interfaceModel = allDefinitions.get(_interface.getSimpleRef());
1223-
if (supportsInheritance) {
1224-
addProperties(allProperties, allRequired, interfaceModel, allDefinitions);
1225-
} else {
1226-
addProperties(properties, required, interfaceModel, allDefinitions);
1227-
}
1228-
}
1229-
}
1230-
}
1241+
12311242
// child model (properties owned by the model itself)
12321243
Model child = composed.getChild();
12331244
if (child != null && child instanceof RefModel && allDefinitions != null) {

modules/swagger-codegen/src/test/java/io/swagger/codegen/java/JavaInheritanceTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
import org.testng.Assert;
1414
import org.testng.annotations.Test;
1515

16+
import java.util.Arrays;
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
1620
public class JavaInheritanceTest {
1721

1822
@SuppressWarnings("static-method")
19-
@Test(description = "convert a composed model")
23+
@Test(description = "convert a composed model with parent")
2024
public void javaInheritanceTest() {
2125
final Model model = new ComposedModel().parent(new RefModel("Base"))
2226
.child(new ModelImpl().additionalProperties(new StringProperty()));
@@ -29,4 +33,26 @@ public void javaInheritanceTest() {
2933
Assert.assertEquals(cm.parent, "Base");
3034
Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
3135
}
36+
37+
@SuppressWarnings("static-method")
38+
@Test(description = "convert a composed model with discriminator")
39+
public void javaInheritanceWithDiscriminatorTest() {
40+
ModelImpl base = new ModelImpl();
41+
base.setDiscriminator("disc");
42+
43+
final Model model = new ComposedModel()
44+
.interfaces(Arrays.asList(new RefModel("Base")))
45+
.child(new ModelImpl().additionalProperties(new StringProperty()));
46+
47+
final Map<String, Model> allDefinitions = new HashMap<String, Model>();
48+
allDefinitions.put("Base", base);
49+
50+
final DefaultCodegen codegen = new JavaClientCodegen();
51+
final CodegenModel cm = codegen.fromModel("sample", model, allDefinitions);
52+
53+
Assert.assertEquals(cm.name, "sample");
54+
Assert.assertEquals(cm.classname, "Sample");
55+
Assert.assertEquals(cm.parent, "Base");
56+
Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
57+
}
3258
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/javascript/JavaScriptInheritanceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void javascriptInheritanceTest() {
5151
Assert.assertEquals(cm.vars.size(), 1);
5252
Assert.assertEquals(cm.vars.get(0).name, "childProp");
5353
Assert.assertEquals(cm.allVars.size(), 4);
54-
String[] allVars = {"baseProp", "intf1Prop", "intf2Prop", "childProp"};
54+
String[] allVars = {"intf1Prop", "intf2Prop", "baseProp", "childProp"};
5555
for (int i = 0; i < allVars.length; i++) {
5656
Assert.assertEquals(cm.allVars.get(i).name, allVars[i]);
5757
}
@@ -91,7 +91,7 @@ public void javascriptNoInheritanceTest() {
9191
Assert.assertEquals(cm.imports, Sets.newHashSet("Base", "Interface1", "Interface2"));
9292
Assert.assertEquals(cm.vars.size(), 4);
9393
Assert.assertEquals(cm.allVars.size(), 4);
94-
String[] allVars = {"baseProp", "intf1Prop", "intf2Prop", "childProp"};
94+
String[] allVars = {"intf1Prop", "intf2Prop", "baseProp", "childProp"};
9595
for (int i = 0; i < allVars.length; i++) {
9696
Assert.assertEquals(cm.vars.get(i).name, allVars[i]);
9797
Assert.assertEquals(cm.allVars.get(i).name, allVars[i]);

0 commit comments

Comments
 (0)