2014-07-05 19:32:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
2014-07-06 14:50:24 +00:00
|
|
|
var _cloneDeep = require('lodash.clonedeep');
|
2014-07-10 18:17:26 +00:00
|
|
|
var translateDataTypeKeys = require('./translate-data-type-keys');
|
2015-04-07 00:00:32 +00:00
|
|
|
var TYPES_PRIMITIVE = ['array', 'boolean', 'integer', 'number', 'null', 'object', 'string', 'any'];
|
2014-07-05 19:32:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the modelHelper singleton.
|
|
|
|
*/
|
|
|
|
var modelHelper = module.exports = {
|
|
|
|
/**
|
|
|
|
* Given a class (from remotes.classes()), generate a model definition.
|
|
|
|
* This is used to generate the schema at the top of many endpoints.
|
2014-07-24 20:35:02 +00:00
|
|
|
* @param {Class} modelClass Model class.
|
|
|
|
* @param {Object} definitions Model definitions
|
|
|
|
* @return {Object} Associated model definition.
|
2014-07-05 19:32:00 +00:00
|
|
|
*/
|
2014-07-24 20:35:02 +00:00
|
|
|
generateModelDefinition: function generateModelDefinition(modelClass, definitions) {
|
2014-10-07 18:57:15 +00:00
|
|
|
var processType = function(app, modelName, referencedModels) {
|
2014-10-07 22:27:49 +00:00
|
|
|
if (app && modelName) {
|
2014-08-06 07:20:15 +00:00
|
|
|
if (modelName.indexOf('[') == 0) {
|
|
|
|
modelName = modelName.replace(/[\[\]]/g, '');
|
|
|
|
}
|
|
|
|
var model = app.models[modelName];
|
2014-10-07 23:52:27 +00:00
|
|
|
if (model && referencedModels.indexOf(model) === -1) {
|
|
|
|
referencedModels.push(model);
|
2014-08-06 07:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-07 00:00:32 +00:00
|
|
|
};
|
|
|
|
|
2015-04-07 02:57:57 +00:00
|
|
|
var convertTypeTo$Ref = function convertTypeTo$Ref(prop){
|
2015-04-07 00:00:32 +00:00
|
|
|
if (prop.type && TYPES_PRIMITIVE.indexOf(prop.type) === -1 ){
|
2015-04-07 02:57:57 +00:00
|
|
|
prop.$ref = prop.type;
|
2015-04-07 00:00:32 +00:00
|
|
|
delete prop.type;
|
|
|
|
}
|
|
|
|
};
|
2014-08-06 07:20:15 +00:00
|
|
|
|
2014-07-24 20:35:02 +00:00
|
|
|
var def = modelClass.definition;
|
2014-07-05 19:32:00 +00:00
|
|
|
var name = def.name;
|
2014-07-24 20:35:02 +00:00
|
|
|
var out = definitions || {};
|
|
|
|
if (out[name]) {
|
|
|
|
// The model is already included
|
|
|
|
return out;
|
|
|
|
}
|
2014-07-05 19:32:00 +00:00
|
|
|
var required = [];
|
2014-07-06 14:50:24 +00:00
|
|
|
// Don't modify original properties.
|
2014-10-07 22:27:49 +00:00
|
|
|
var properties = _cloneDeep(def.rawProperties || def.properties);
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-08-28 22:20:03 +00:00
|
|
|
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) {
|
2014-10-07 23:52:27 +00:00
|
|
|
if (model && referencedModels.indexOf(model) === -1) {
|
2014-08-28 22:20:03 +00:00
|
|
|
referencedModels.push(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:32:00 +00:00
|
|
|
// Iterate through each property in the model definition.
|
2014-07-10 16:32:58 +00:00
|
|
|
// Types may be defined as constructors (e.g. String, Date, etc.),
|
|
|
|
// or as strings; getPropType() will take care of the conversion.
|
|
|
|
// See more on types:
|
|
|
|
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#431-primitives
|
|
|
|
Object.keys(properties).forEach(function(key) {
|
2014-07-06 14:50:24 +00:00
|
|
|
var prop = properties[key];
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-26 17:12:05 +00:00
|
|
|
// Hide hidden properties.
|
|
|
|
if (modelHelper.isHiddenProperty(def, key)) {
|
|
|
|
delete properties[key];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:32:00 +00:00
|
|
|
// Eke a type out of the constructors we were passed.
|
2014-07-10 16:32:58 +00:00
|
|
|
prop = modelHelper.LDLPropToSwaggerDataType(prop);
|
2014-10-07 22:27:49 +00:00
|
|
|
processType(modelClass.app, prop.type, referencedModels);
|
2014-08-06 07:31:34 +00:00
|
|
|
if (prop.items) {
|
2014-10-07 22:27:49 +00:00
|
|
|
processType(modelClass.app, prop.items.type, referencedModels);
|
2015-04-07 02:57:57 +00:00
|
|
|
convertTypeTo$Ref(prop.items);
|
2014-08-06 07:31:34 +00:00
|
|
|
}
|
2014-07-05 19:32:00 +00:00
|
|
|
|
|
|
|
// Required props sit in a per-model array.
|
2015-04-07 00:00:32 +00:00
|
|
|
if (prop.required || (prop.id && !prop.generated)) {
|
2014-07-05 19:32:00 +00:00
|
|
|
required.push(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change mismatched keys.
|
2014-07-10 18:17:26 +00:00
|
|
|
prop = translateDataTypeKeys(prop);
|
2014-07-10 16:32:58 +00:00
|
|
|
|
2015-04-07 02:57:57 +00:00
|
|
|
convertTypeTo$Ref(prop);
|
2015-03-27 05:23:24 +00:00
|
|
|
|
2015-04-05 23:51:15 +00:00
|
|
|
delete prop.required;
|
|
|
|
delete prop.id;
|
|
|
|
|
2015-04-07 00:00:32 +00:00
|
|
|
if (prop.description) {
|
|
|
|
prop.description = Array.isArray(prop.description) ? prop.description.join('') : prop.description;
|
|
|
|
}
|
|
|
|
|
2014-07-10 16:32:58 +00:00
|
|
|
// Assign this back to the properties object.
|
|
|
|
properties[key] = prop;
|
2014-08-28 22:20:03 +00:00
|
|
|
|
|
|
|
var propType = def.properties[key].type;
|
|
|
|
if (typeof propType === 'function' && propType.modelName) {
|
2014-08-29 08:06:05 +00:00
|
|
|
if (referencedModels.indexOf(propType) === -1) {
|
2014-08-28 22:20:03 +00:00
|
|
|
referencedModels.push(propType);
|
|
|
|
}
|
|
|
|
}
|
2014-08-29 08:06:05 +00:00
|
|
|
if (Array.isArray(propType) && propType.length) {
|
|
|
|
var itemType = propType[0];
|
|
|
|
if (typeof itemType === 'function' && itemType.modelName) {
|
|
|
|
if (referencedModels.indexOf(itemType) === -1) {
|
|
|
|
referencedModels.push(itemType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-05 19:32:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
out[name] = {
|
2015-04-07 02:57:57 +00:00
|
|
|
id: name,
|
2014-07-06 14:50:24 +00:00
|
|
|
properties: properties,
|
2015-01-10 05:52:57 +00:00
|
|
|
required: required
|
2014-07-05 19:32:00 +00:00
|
|
|
};
|
2015-04-07 00:00:32 +00:00
|
|
|
|
|
|
|
if (def.description){
|
|
|
|
out[name].description = Array.isArray(def.description) ? def.description.join('') : def.description;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (def.settings && typeof def.settings.strict != 'undefined') {
|
|
|
|
out[name].additionalProperties = !def.settings.strict;
|
|
|
|
} else if (def.settings && typeof def.settings.additionalProperties != 'undefined') {
|
2015-01-10 05:52:57 +00:00
|
|
|
out[name].additionalProperties = def.settings.additionalProperties;
|
|
|
|
}
|
2014-07-24 20:35:02 +00:00
|
|
|
|
|
|
|
// Generate model definitions for related models
|
|
|
|
for (var r in modelClass.relations) {
|
|
|
|
var rel = modelClass.relations[r];
|
2014-10-07 23:52:27 +00:00
|
|
|
if (rel.modelTo && referencedModels.indexOf(rel.modelTo) === -1) {
|
|
|
|
referencedModels.push(rel.modelTo);
|
2014-08-10 06:46:24 +00:00
|
|
|
}
|
2014-10-07 23:52:27 +00:00
|
|
|
if (rel.modelThrough && referencedModels.indexOf(rel.modelThrough) === -1) {
|
|
|
|
referencedModels.push(rel.modelThrough);
|
2014-07-24 20:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-06 07:20:15 +00:00
|
|
|
|
2014-10-07 22:27:49 +00:00
|
|
|
if (modelClass.sharedClass) {
|
|
|
|
var remotes = modelClass.sharedClass.methods();
|
|
|
|
for (var remoteIdx in remotes) {
|
|
|
|
var remote = remotes[remoteIdx];
|
|
|
|
var accepts = remote.accepts;
|
|
|
|
if (accepts) {
|
|
|
|
for (var acceptIdx in accepts) {
|
|
|
|
processType(modelClass.app, accepts[acceptIdx].type, referencedModels);
|
|
|
|
}
|
2014-09-18 02:02:39 +00:00
|
|
|
}
|
2014-10-07 22:27:49 +00:00
|
|
|
var returns = remote.returns;
|
|
|
|
if (returns) {
|
|
|
|
for (var returnIdx in returns) {
|
|
|
|
processType(modelClass.app, returns[returnIdx].type, referencedModels);
|
|
|
|
}
|
2014-08-06 07:20:15 +00:00
|
|
|
}
|
2014-10-07 22:27:49 +00:00
|
|
|
var errors = remote.errors;
|
|
|
|
if (errors) {
|
|
|
|
for (var errorIdx in errors) {
|
|
|
|
processType(modelClass.app, errors[errorIdx].responseModel, referencedModels);
|
|
|
|
}
|
2014-08-06 07:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 18:57:15 +00:00
|
|
|
|
2014-09-19 16:35:11 +00:00
|
|
|
for (var i = 0, n = referencedModels.length; i < n; i++) {
|
2014-10-07 23:52:27 +00:00
|
|
|
if (referencedModels[i].definition) {
|
|
|
|
generateModelDefinition(referencedModels[i], out);
|
|
|
|
}
|
2014-08-28 22:20:03 +00:00
|
|
|
}
|
2014-07-05 19:32:00 +00:00
|
|
|
return out;
|
2014-07-10 16:32:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a propType (which may be a function, string, or array),
|
|
|
|
* get a string type.
|
|
|
|
* @param {*} propType Prop type description.
|
|
|
|
* @return {String} Prop type string.
|
2014-10-07 23:52:27 +00:00
|
|
|
*/
|
2014-07-10 16:32:58 +00:00
|
|
|
getPropType: function getPropType(propType) {
|
|
|
|
if (typeof propType === 'function') {
|
2014-08-05 06:22:41 +00:00
|
|
|
// See https://github.com/strongloop/loopback-explorer/issues/32
|
|
|
|
// The type can be a model class
|
|
|
|
propType = propType.modelName || propType.name.toLowerCase();
|
2014-07-10 16:32:58 +00:00
|
|
|
} else if(Array.isArray(propType)) {
|
|
|
|
propType = 'array';
|
|
|
|
}
|
|
|
|
return propType;
|
|
|
|
},
|
|
|
|
|
2014-07-26 17:12:05 +00:00
|
|
|
isHiddenProperty: function(definition, propName) {
|
2014-10-07 23:52:27 +00:00
|
|
|
return definition.settings &&
|
2014-07-26 17:12:05 +00:00
|
|
|
Array.isArray(definition.settings.hidden) &&
|
|
|
|
definition.settings.hidden.indexOf(propName) !== -1;
|
|
|
|
},
|
|
|
|
|
2014-07-10 16:32:58 +00:00
|
|
|
// Converts a prop defined with the LDL spec to one conforming to the
|
|
|
|
// Swagger spec.
|
|
|
|
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#431-primitives
|
|
|
|
LDLPropToSwaggerDataType: function LDLPropToSwaggerDataType(prop) {
|
2014-10-07 22:27:49 +00:00
|
|
|
if (typeof prop === 'string') {
|
|
|
|
prop = {
|
|
|
|
type: prop
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 16:32:58 +00:00
|
|
|
var out = _cloneDeep(prop);
|
|
|
|
out.type = modelHelper.getPropType(out.type);
|
|
|
|
|
|
|
|
if (out.type === 'array') {
|
2014-07-31 22:52:52 +00:00
|
|
|
var hasItemType = Array.isArray(prop.type) && prop.type.length;
|
|
|
|
var arrayItem = hasItemType && prop.type[0];
|
|
|
|
|
|
|
|
if (arrayItem) {
|
|
|
|
if(typeof arrayItem === 'object') {
|
|
|
|
out.items = modelHelper.LDLPropToSwaggerDataType(arrayItem);
|
|
|
|
} else {
|
|
|
|
out.items = { type: modelHelper.getPropType(arrayItem) };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// NOTE: `any` is not a supported type in swagger 1.2
|
|
|
|
out.items = { type: 'any' };
|
2014-07-22 19:34:42 +00:00
|
|
|
}
|
|
|
|
} else if (out.type === 'date') {
|
2014-07-10 16:32:58 +00:00
|
|
|
out.type = 'string';
|
|
|
|
out.format = 'date';
|
|
|
|
} else if (out.type === 'buffer') {
|
|
|
|
out.type = 'string';
|
|
|
|
out.format = 'byte';
|
|
|
|
} else if (out.type === 'number') {
|
|
|
|
out.format = 'double'; // Since all JS numbers are doubles
|
|
|
|
}
|
|
|
|
return out;
|
2014-07-05 19:32:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-10 16:32:58 +00:00
|
|
|
|