Make sure nested/referenced models in array are mapped to swagger

Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
Clark Wang 2014-08-29 16:06:05 +08:00
parent d083feab36
commit acf00f3254
2 changed files with 20 additions and 1 deletions

View File

@ -72,10 +72,18 @@ var modelHelper = module.exports = {
var propType = def.properties[key].type; var propType = def.properties[key].type;
if (typeof propType === 'function' && propType.modelName) { if (typeof propType === 'function' && propType.modelName) {
if (referencedModels.indexOf(m) === -1) { if (referencedModels.indexOf(propType) === -1) {
referencedModels.push(propType); 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] = {

View File

@ -153,6 +153,17 @@ describe('model-helper', function() {
expect(defs).has.property('Model3'); expect(defs).has.property('Model3');
expect(defs).has.property('Model4'); 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() {