From 67bb10b6a4b57f9efcdfb3fd4e30fc58ef6bb5ad Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Sat, 5 Jul 2014 10:54:19 -0500 Subject: [PATCH] Some swagger 1.2 migration cleanup. - Added array items definition. - Ignored body when specified via arg.http.source. --- lib/swagger.js | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/swagger.js b/lib/swagger.js index 936fb2e..823c1d0 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -73,6 +73,7 @@ function Swagger(remotes, options) { return; } + // Get the class definition matching this route. classDef = classes.filter(function (item) { return item.name === split[0]; })[0]; @@ -81,12 +82,14 @@ function Swagger(remotes, options) { route.accepts = (route.accepts || []).concat(classDef.sharedCtor.accepts); } + // Filter out parameters that are generated from the incoming request or body, + // or generated by functions that use those resources. route.accepts = (route.accepts || []).filter(function(arg){ if (!arg.http) return true; // Don't show derived arguments. if (typeof arg.http === 'function') return false; - // Don't show arguments set to the incoming http request. - if (arg.http.source === 'req') return false; + // Don't show arguments set to the incoming http request or body. + if (arg.http.source === 'req' || arg.http.source === 'body') return false; return true; }); @@ -200,22 +203,26 @@ function generateModelDefinition(aClass) { 'min': 'minimum', 'max': 'maximum' }; + // 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]; - // Required props sit in a different array. + + // Eke a type out of the constructors we were passed. + prop.type = getPropType(prop.type); + if (prop.type === 'array') { + prop.items = { + type: getPropType(prop.type[0]) + }; + } + + // Required props sit in a per-model array. if (prop.required || prop.id) { required.push(key); } - // Eke a type out of the constructors we were passed. - if (typeof prop.type === "function") { - prop.type = prop.type.name.toLowerCase(); - } else if(Array.isArray(prop.type)) { - prop.type = 'array'; - // prop.items.type = prop.type[0] ? prop.type[0].name.toLowerCase(): 'any'; - } + // Change mismatched keys. Object.keys(keyTranslations).forEach(function(LDLKey){ var val = prop[LDLKey]; @@ -239,6 +246,21 @@ function generateModelDefinition(aClass) { return out; } +/** + * 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. + */ +function getPropType(propType) { + if (typeof propType === "function") { + propType = propType.name.toLowerCase(); + } else if(Array.isArray(propType)) { + propType = 'array'; + } + return propType; +} + /** * Converts from an sl-remoting-formatted "Route" description to a * Swagger-formatted "API" description. @@ -247,8 +269,6 @@ function generateModelDefinition(aClass) { function routeToAPI(route) { var returnDesc = route.returns && route.returns[0]; - console.log(route); - return { path: convertPathFragments(route.path), operations: [{