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:
cbornet
2016-05-30 18:44:44 +02:00
committed by Tony Tam
parent bd0340327b
commit 8740b4d0ea
3 changed files with 59 additions and 22 deletions

View File

@@ -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) {