forked from loafle/openapi-generator-original
Merge pull request #3637 from cbornet/fix_multi_inheritance
[All] Fix some inheritance/composition issues and add allOf unit tests
This commit is contained in:
@@ -100,6 +100,7 @@ public class DefaultCodegen {
|
||||
protected List<CliOption> cliOptions = new ArrayList<CliOption>();
|
||||
protected boolean skipOverwrite;
|
||||
protected boolean supportsInheritance;
|
||||
protected boolean supportsMixins;
|
||||
protected Map<String, String> supportedLibraries = new LinkedHashMap<String, String>();
|
||||
protected String library;
|
||||
protected Boolean sortParamsByRequiredFlag = true;
|
||||
@@ -1235,7 +1236,7 @@ public class DefaultCodegen {
|
||||
List<String> required = new ArrayList<String>();
|
||||
Map<String, Property> allProperties;
|
||||
List<String> allRequired;
|
||||
if (supportsInheritance) {
|
||||
if (supportsInheritance || supportsMixins) {
|
||||
allProperties = new LinkedHashMap<String, Property>();
|
||||
allRequired = new ArrayList<String>();
|
||||
m.allVars = new ArrayList<CodegenProperty>();
|
||||
@@ -1256,17 +1257,20 @@ public class DefaultCodegen {
|
||||
interfaceModel = allDefinitions.get(_interface.getSimpleRef());
|
||||
}
|
||||
// set first interface with discriminator found as parent
|
||||
if (parent == null && interfaceModel instanceof ModelImpl && ((ModelImpl) interfaceModel).getDiscriminator() != null) {
|
||||
if (parent == null
|
||||
&& ((interfaceModel instanceof ModelImpl && ((ModelImpl) interfaceModel).getDiscriminator() != null)
|
||||
|| (interfaceModel instanceof ComposedModel && isDiscriminatorInInterfaceTree((ComposedModel) interfaceModel, allDefinitions)))) {
|
||||
parent = _interface;
|
||||
} else {
|
||||
final String interfaceRef = toModelName(_interface.getSimpleRef());
|
||||
m.interfaces.add(interfaceRef);
|
||||
addImport(m, interfaceRef);
|
||||
if (allDefinitions != null) {
|
||||
if (!supportsMixins) {
|
||||
addProperties(properties, required, interfaceModel, allDefinitions);
|
||||
}
|
||||
if (supportsInheritance) {
|
||||
addProperties(allProperties, allRequired, interfaceModel, allDefinitions);
|
||||
} else {
|
||||
addProperties(properties, required, interfaceModel, allDefinitions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1325,6 +1329,30 @@ public class DefaultCodegen {
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively look for a discriminator in the interface tree
|
||||
*/
|
||||
private boolean isDiscriminatorInInterfaceTree(ComposedModel model, Map<String, Model> allDefinitions) {
|
||||
if (model == null || allDefinitions == null)
|
||||
return false;
|
||||
|
||||
Model child = ((ComposedModel) model).getChild();
|
||||
if (child instanceof ModelImpl && ((ModelImpl) child).getDiscriminator() != null) {
|
||||
return true;
|
||||
}
|
||||
for (RefModel _interface : model.getInterfaces()) {
|
||||
Model interfaceModel = allDefinitions.get(_interface.getSimpleRef());
|
||||
if (interfaceModel instanceof ModelImpl && ((ModelImpl) interfaceModel).getDiscriminator() != null) {
|
||||
return true;
|
||||
}
|
||||
if (interfaceModel instanceof ComposedModel) {
|
||||
|
||||
return isDiscriminatorInInterfaceTree((ComposedModel) interfaceModel, allDefinitions);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) {
|
||||
MapProperty mapProperty = new MapProperty(swaggerModel.getAdditionalProperties());
|
||||
addParentContainer(codegenModel, codegenModel.name, mapProperty);
|
||||
|
||||
@@ -234,6 +234,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
setUseInheritance(Boolean.parseBoolean((String)additionalProperties.get(USE_INHERITANCE)));
|
||||
} else {
|
||||
supportsInheritance = true;
|
||||
supportsMixins = true;
|
||||
}
|
||||
if (additionalProperties.containsKey(EMIT_MODEL_METHODS)) {
|
||||
setEmitModelMethods(Boolean.parseBoolean((String)additionalProperties.get(EMIT_MODEL_METHODS)));
|
||||
@@ -397,6 +398,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
|
||||
public void setUseInheritance(boolean useInheritance) {
|
||||
this.supportsInheritance = useInheritance;
|
||||
this.supportsMixins = useInheritance;
|
||||
}
|
||||
|
||||
public void setEmitModelMethods(boolean emitModelMethods) {
|
||||
@@ -900,7 +902,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
// NOTE: can't use 'mandatory' as it is built from ModelImpl.getRequired(), which sorts names
|
||||
// alphabetically and in any case the document order of 'required' and 'properties' can differ.
|
||||
List<CodegenProperty> required = new ArrayList<>();
|
||||
List<CodegenProperty> allRequired = supportsInheritance ? new ArrayList<CodegenProperty>() : required;
|
||||
List<CodegenProperty> allRequired = supportsInheritance || supportsMixins ? new ArrayList<CodegenProperty>() : required;
|
||||
cm.vendorExtensions.put("x-required", required);
|
||||
cm.vendorExtensions.put("x-all-required", allRequired);
|
||||
|
||||
@@ -914,7 +916,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
}
|
||||
}
|
||||
|
||||
if (supportsInheritance) {
|
||||
if (supportsInheritance || supportsMixins) {
|
||||
for (CodegenProperty var : cm.allVars) {
|
||||
if (Boolean.TRUE.equals(var.required)) {
|
||||
allRequired.add(var);
|
||||
|
||||
Reference in New Issue
Block a user