Merge branch 'release/1.2.10' into production

This commit is contained in:
Raymond Feng 2014-09-05 15:45:26 -07:00
commit 72d0f9e4d6
3 changed files with 67 additions and 2 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

@ -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": {

View File

@ -123,6 +123,7 @@ describe('model-helper', function() {
});
});
describe('related models', function() {
it('should include related models', function () {
var defs = buildSwaggerModelsWithRelations({
@ -131,7 +132,40 @@ describe('model-helper', function() {
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({