Skip to content

Commit

Permalink
Update processing for allOf
Browse files Browse the repository at this point in the history
- Inherit from multiple interfaces
- Ignore x-nullable in a property's allOf definition
  • Loading branch information
David Coblentz committed Mar 6, 2020
1 parent b6542c2 commit 3cef0a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
43 changes: 27 additions & 16 deletions ng-swagger-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ function processModels(swagger, options) {
var models = {};
for (name in swagger.definitions) {
model = swagger.definitions[name];
var parent = null;
var parents = null;
var properties = null;
var requiredProperties = null;
var additionalPropertiesType = false;
Expand All @@ -607,10 +607,11 @@ function processModels(swagger, options) {
var elementType = null;
var simpleType = null;
if (model.allOf != null && model.allOf.length > 0) {
parent = simpleRef((model.allOf[0] || {}).$ref);
properties = (model.allOf[1] || {}).properties || {};
requiredProperties = (model.allOf[1] || {}).required || [];
} else if (model.type === 'string') {
parents = model.allOf
.filter(parent => !!parent.$ref)
.map(parent => simpleRef(parent.$ref));
properties = (model.allOf.find(val => !!val.properties) || {}).properties || {};
requiredProperties = (model.allOf.find(val => !!val.required) || {}).required || [];
enumValues = model.enum || [];
if (enumValues.length == 0) {
simpleType = 'string';
Expand Down Expand Up @@ -649,7 +650,7 @@ function processModels(swagger, options) {
modelClass: modelClass,
modelFile: toFileName(modelClass) + options.customFileSuffix.model,
modelComments: toComments(model.description),
modelParent: parent,
modelParents: parents,
modelIsObject: properties != null,
modelIsEnum: enumValues != null,
modelIsArray: elementType != null,
Expand Down Expand Up @@ -695,13 +696,19 @@ function processModels(swagger, options) {
}

// Process the hierarchy
var parentName = model.modelParent;
if (parentName) {
// Make the parent be the actual model, not the name
model.modelParent = models[normalizeModelName(parentName)];

// Append this model on the parent's subclasses
model.modelParent.modelSubclasses.push(model);
var parentNames = model.modelParents;
if (parentNames && parentNames.length > 0) {
model.modelParents = parentNames
.filter(parentName => !!parentName)
.map(parentName => {
// Make the parent be the actual model, not the name
var parentModel = models[normalizeModelName(parentName)];

// Append this model on the parent's subclasses
parentModel.modelSubclasses.push(model);
return parentModel;
});
model.modelParents[0].parentIsFirst = true;
}
}

Expand All @@ -721,8 +728,10 @@ function processModels(swagger, options) {
var dependencies = new DependenciesResolver(models, model.modelName);

// The parent is a dependency
if (model.modelParent) {
dependencies.add(model.modelParent.modelName);
if (model.modelParents) {
model.modelParents.forEach(modelParent => {
dependencies.add(modelParent.modelName);
})
}

// Each property may add a dependency
Expand Down Expand Up @@ -822,7 +831,9 @@ function propertyType(property) {
toString: () => variants.join(' | ')
};
} else if (!property.type && property.allOf) {
let variants = (property.allOf).map(propertyType);
// Do not want to include x-nullable types as part of an allOf union.
let variants = (property.allOf).filter(prop => !prop['x-nullable']).map(propertyType);

return {
allTypes: mergeTypes(...variants),
toString: () => variants.join(' & ')
Expand Down
2 changes: 1 addition & 1 deletion templates/object.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{{modelComments}}}export interface {{modelClass}} {{#modelParent}}extends {{modelClass}} {{/modelParent}}{
{{{modelComments}}}export interface {{modelClass}} {{#modelParents}}{{#parentIsFirst}}extends {{/parentIsFirst}}{{^parentIsFirst}}, {{/parentIsFirst}}{{modelClass}}{{/modelParents}} {
{{#modelProperties}}
{{{propertyComments}}}{{&propertyName}}{{^propertyRequired}}?{{/propertyRequired}}: {{{propertyType}}};
{{/modelProperties}}
Expand Down

0 comments on commit 3cef0a3

Please sign in to comment.