Merge pull request #44 from strongloop/feature/add-other-models

Make sure nested/referenced models are mapped to swagger
This commit is contained in:
Raymond Feng 2014-09-05 15:40:27 -07:00
commit ea4dc17e58
2 changed files with 66 additions and 1 deletions

View File

@ -29,6 +29,19 @@ var modelHelper = module.exports = {
// Don't modify original 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.
// Types may be defined as constructors (e.g. String, Date, etc.),
// 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.
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] = {
@ -74,6 +102,9 @@ var modelHelper = module.exports = {
generateModelDefinition(rel.modelThrough, out);
}
}
for(var rm in referencedModels) {
generateModelDefinition(referencedModels[rm], out);
}
return out;
},

View File

@ -123,15 +123,49 @@ describe('model-helper', function() {
});
});
describe('related models', function() {
it('should include related models', function() {
it('should include related models', function () {
var defs = buildSwaggerModelsWithRelations({
str: String // 'string'
});
expect(defs).has.property('testModel');
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() {
it('should hide properties marked as "hidden"', function() {
var aClass = createModelCtor({