Refactor key translations between LDL & Swagger.

Route.accepts & route.returns can now share these translations.
This commit is contained in:
Samuel Reed 2014-07-06 09:50:24 -05:00 committed by Samuel Reed
parent 4c0ce42001
commit a4a36f5602
3 changed files with 50 additions and 26 deletions

View File

@ -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;

View File

@ -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
};

35
lib/translate-keys.js Normal file
View File

@ -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;
};