Merge pull request #44 from strongloop/feature/add-other-models
Make sure nested/referenced models are mapped to swagger
This commit is contained in:
commit
ea4dc17e58
|
@ -29,6 +29,19 @@ var modelHelper = module.exports = {
|
||||||
// Don't modify original properties.
|
// Don't modify original properties.
|
||||||
var properties = _cloneDeep(def.properties);
|
var properties = _cloneDeep(def.properties);
|
||||||
|
|
||||||
|
var referencedModels = [];
|
||||||
|
// Add models from settings
|
||||||
|
if (def.settings && def.settings.models) {
|
||||||
|
for (var m in def.settings.models) {
|
||||||
|
var model = modelClass[m];
|
||||||
|
if (typeof model === 'function' && model.modelName) {
|
||||||
|
if (referencedModels.indexOf(model) === -1) {
|
||||||
|
referencedModels.push(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Iterate through each property in the model definition.
|
// Iterate through each property in the model definition.
|
||||||
// Types may be defined as constructors (e.g. String, Date, etc.),
|
// Types may be defined as constructors (e.g. String, Date, etc.),
|
||||||
// or as strings; getPropType() will take care of the conversion.
|
// or as strings; getPropType() will take care of the conversion.
|
||||||
|
@ -56,6 +69,21 @@ var modelHelper = module.exports = {
|
||||||
|
|
||||||
// Assign this back to the properties object.
|
// Assign this back to the properties object.
|
||||||
properties[key] = prop;
|
properties[key] = prop;
|
||||||
|
|
||||||
|
var propType = def.properties[key].type;
|
||||||
|
if (typeof propType === 'function' && propType.modelName) {
|
||||||
|
if (referencedModels.indexOf(propType) === -1) {
|
||||||
|
referencedModels.push(propType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Array.isArray(propType) && propType.length) {
|
||||||
|
var itemType = propType[0];
|
||||||
|
if (typeof itemType === 'function' && itemType.modelName) {
|
||||||
|
if (referencedModels.indexOf(itemType) === -1) {
|
||||||
|
referencedModels.push(itemType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
out[name] = {
|
out[name] = {
|
||||||
|
@ -74,6 +102,9 @@ var modelHelper = module.exports = {
|
||||||
generateModelDefinition(rel.modelThrough, out);
|
generateModelDefinition(rel.modelThrough, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for(var rm in referencedModels) {
|
||||||
|
generateModelDefinition(referencedModels[rm], out);
|
||||||
|
}
|
||||||
return out;
|
return out;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -123,6 +123,7 @@ describe('model-helper', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('related models', function() {
|
describe('related models', function() {
|
||||||
it('should include related models', function () {
|
it('should include related models', function () {
|
||||||
var defs = buildSwaggerModelsWithRelations({
|
var defs = buildSwaggerModelsWithRelations({
|
||||||
|
@ -131,7 +132,40 @@ describe('model-helper', function() {
|
||||||
expect(defs).has.property('testModel');
|
expect(defs).has.property('testModel');
|
||||||
expect(defs).has.property('relatedModel');
|
expect(defs).has.property('relatedModel');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should include nesting models', function() {
|
||||||
|
var Model2 = loopback.createModel('Model2', {street: String});
|
||||||
|
var Model1 = loopback.createModel('Model1', {
|
||||||
|
str: String, // 'string'
|
||||||
|
address: Model2
|
||||||
});
|
});
|
||||||
|
var defs = modelHelper.generateModelDefinition(Model1, {});
|
||||||
|
expect(defs).has.property('Model1');
|
||||||
|
expect(defs).has.property('Model2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should include used models', function() {
|
||||||
|
var Model4 = loopback.createModel('Model4', {street: String});
|
||||||
|
var Model3 = loopback.createModel('Model3', {
|
||||||
|
str: String, // 'string'
|
||||||
|
}, {models: {model4: 'Model4'}});
|
||||||
|
var defs = modelHelper.generateModelDefinition(Model3, {});
|
||||||
|
expect(defs).has.property('Model3');
|
||||||
|
expect(defs).has.property('Model4');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should include nesting models in array', function() {
|
||||||
|
var Model6 = loopback.createModel('Model6', {street: String});
|
||||||
|
var Model5 = loopback.createModel('Model5', {
|
||||||
|
str: String, // 'string'
|
||||||
|
addresses: [Model6]
|
||||||
|
});
|
||||||
|
var defs = modelHelper.generateModelDefinition(Model5, {});
|
||||||
|
expect(defs).has.property('Model5');
|
||||||
|
expect(defs).has.property('Model6');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('hidden properties', function() {
|
describe('hidden properties', function() {
|
||||||
it('should hide properties marked as "hidden"', function() {
|
it('should hide properties marked as "hidden"', function() {
|
||||||
var aClass = createModelCtor({
|
var aClass = createModelCtor({
|
||||||
|
|
Loading…
Reference in New Issue