diff --git a/lib/model-helper.js b/lib/model-helper.js index 5820550..1fafcbd 100644 --- a/lib/model-helper.js +++ b/lib/model-helper.js @@ -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; }, diff --git a/package.json b/package.json index abe35aa..9bef826 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-explorer", - "version": "1.2.9", + "version": "1.2.10", "description": "Browse and test your LoopBack app's APIs", "main": "index.js", "scripts": { diff --git a/test/model-helper.test.js b/test/model-helper.test.js index e376a2a..efe720a 100644 --- a/test/model-helper.test.js +++ b/test/model-helper.test.js @@ -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({