diff --git a/lib/model-helper.js b/lib/model-helper.js index ab532bb..7108341 100644 --- a/lib/model-helper.js +++ b/lib/model-helper.js @@ -3,19 +3,8 @@ /** * Module dependencies. */ - -/** - * Constants - */ - - // Keys that are different between LDL and Swagger - var KEY_TRANSLATIONS = { - // LDL : Swagger - 'doc': 'description', - 'default': 'defaultValue', - 'min': 'minimum', - 'max': 'maximum' - }; +var _cloneDeep = require('lodash.clonedeep'); +var translateKeys = require('./translate-keys'); /** * Export the modelHelper singleton. @@ -32,12 +21,14 @@ var modelHelper = module.exports = { var name = def.name; var required = []; + // Don't modify original properties. + var properties = _cloneDeep(def.properties); // Iterate through each property in the model definition. // Types are defined as constructors (e.g. String, Date, etc.) // so we convert them to strings. Object.keys(def.properties).forEach(function(key) { - var prop = def.properties[key]; + var prop = properties[key]; // Eke a type out of the constructors we were passed. prop.type = getPropType(prop.type); @@ -53,23 +44,13 @@ var modelHelper = module.exports = { } // Change mismatched keys. - Object.keys(KEY_TRANSLATIONS).forEach(function(LDLKey){ - var val = prop[LDLKey]; - if (val) { - // Should change in Swagger 2.0 - if (LDLKey === 'min' || LDLKey === 'max') { - val = String(val); - } - prop[KEY_TRANSLATIONS[LDLKey]] = val; - } - delete prop[LDLKey]; - }); + prop = translateKeys(prop); }); var out = {}; out[name] = { id: name, - properties: def.properties, + properties: properties, required: required }; return out; diff --git a/lib/route-helper.js b/lib/route-helper.js index 7010647..ff01fb6 100644 --- a/lib/route-helper.js +++ b/lib/route-helper.js @@ -6,6 +6,7 @@ var debug = require('debug')('loopback-explorer:routeHelpers'); var _cloneDeep = require('lodash.clonedeep'); +var translateKeys = require('./translate-keys'); /** * Export the routeHelper singleton. @@ -94,6 +95,10 @@ function doRouteParameterHacks(route, classDef) { } } + // Translate LDL keys to Swagger keys. + route.accepts = (route.accepts || []).map(translateKeys); + route.returns = (route.returns || []).map(translateKeys); + debug('route %j', route); return route; @@ -176,6 +181,9 @@ function acceptToParameter(route) { description: accepts.description, type: accepts.model || prepareDataType(accepts.type), required: !!accepts.required, + defaultValue: accepts.defaultValue, + minimum: accepts.minimum, + maximum: accepts.maximum, allowMultiple: false }; diff --git a/lib/translate-keys.js b/lib/translate-keys.js new file mode 100644 index 0000000..20bb729 --- /dev/null +++ b/lib/translate-keys.js @@ -0,0 +1,35 @@ +'use strict'; + +/** + * Module dependencies. + */ + +// Keys that are different between LDL and Swagger +var KEY_TRANSLATIONS = { + // LDL : Swagger + 'doc': 'description', + 'default': 'defaultValue', + 'min': 'minimum', + 'max': 'maximum' +}; + +/** + * Correct key mismatches between LDL & Swagger. + * Will modify original object. + * @param {Object} object Object on which to change keys. + * @return {Object} Translated object. + */ +module.exports = function translateKeys(object) { + Object.keys(KEY_TRANSLATIONS).forEach(function(LDLKey){ + var val = object[LDLKey]; + if (val) { + // Should change in Swagger 2.0 + if (LDLKey === 'min' || LDLKey === 'max') { + val = String(val); + } + object[KEY_TRANSLATIONS[LDLKey]] = val; + } + delete object[LDLKey]; + }); + return object; +};