From 02ea0ac25fb3e6d6f24311af46316da52fb86434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=A4=91=ED=9B=88?= Date: Wed, 22 Jun 2016 02:49:15 +0900 Subject: [PATCH 1/2] fix missing validators of referenced parameters - moved merge logic into validators, removing temporal map - added testcase and fixed some tests for added path in pets.json --- lib/buildroutes.js | 26 ++++++------- lib/validator.js | 15 +++++--- test/fixtures/defs/pets.json | 61 +++++++++++++++++++++++++++++++ test/fixtures/extensions/items.js | 5 +++ test/test-routebuilder.js | 10 +++-- test/test-swaggerize.js | 2 +- 6 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 test/fixtures/extensions/items.js diff --git a/lib/buildroutes.js b/lib/buildroutes.js index 515bee7..ad1ef9d 100644 --- a/lib/buildroutes.js +++ b/lib/buildroutes.js @@ -48,19 +48,19 @@ function buildroutes(options) { jsonp: operation.jsonp || api.jsonp }; - validators = {}; - - if (def.parameters) { - def.parameters.forEach(function (parameter) { - validators[parameter.in + parameter.name] = parameter; - }); - } - - if (operation.parameters) { - operation.parameters.forEach(function (parameter) { - validators[parameter.in + parameter.name] = parameter; - }); - } + validators = [].concat ( def.parameters || [], operation.parameters || [] ); + // validators = {}; + // if (def.parameters) { + // def.parameters.forEach(function (parameter) { + // validators[parameter.in + parameter.name] = parameter; + // }); + // } + // + // if (operation.parameters) { + // operation.parameters.forEach(function (parameter) { + // validators[parameter.in + parameter.name] = parameter; + // }); + // } route.validators = validator.makeAll(validators, route); diff --git a/lib/validator.js b/lib/validator.js index 68b2cd0..bb49ad6 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -44,17 +44,22 @@ module.exports = function validator(options) { return { /** - * Creates a parameter validator. + * Creates all validators from given parameter array. * @param parameter * @returns {Function} */ makeAll: function (validators, route) { var self = this; - - return Object.keys(validators).map(function (k) { + var madeAll = {}; + Object.keys(validators).forEach(function (k) { var parameter = validators[k]; + var made = self.make(parameter, route.consumes); + var key = made.parameter.in + made.parameter.name; + madeAll[key] = made; + }); - return self.make(parameter, route.consumes); + return Object.keys(madeAll).map(function (k) { + return madeAll[k]; }); }, @@ -100,7 +105,7 @@ module.exports = function validator(options) { } if (parameter.in !== 'body' && parameter.allowEmptyValue){ - schema = schema.allow('').optional(); + schema = schema.allow('').optional(); } return { diff --git a/test/fixtures/defs/pets.json b/test/fixtures/defs/pets.json index 01f78d7..3e22486 100644 --- a/test/fixtures/defs/pets.json +++ b/test/fixtures/defs/pets.json @@ -275,7 +275,60 @@ "format": "dateTime" } ] + }, + "/pets/{id}/items/{itemId}": { + "put": { + "description": "update some pet's item", + "operationId": "updatePetItem", + "x-handler": "extensions/items.js", + "produces": [ + "application/json" + ], + "security": [ + { + "default": ["read"] + } + ], + "parameters": [ + { + "name": "item", + "in": "body", + "description": "Item to add to the store", + "required": true, + "schema": { + "$ref": "#/definitions/Item" + } + } + ], + "responses": { + "200": { + "description": "old item, before update", + "schema": { + "$ref": "#/definitions/Item" + } + }, + "default": { + "description": "unexpected error", + "schema": { + "$ref": "#/definitions/Error" + } + } + } + }, + "parameters": [ + {"$ref": "#/parameters/id"}, + {"$ref": "#/parameters/itemId"}, + { + "name": "date", + "in": "header", + "description": "Operation date", + "required": false, + "type": "string", + "format": "dateTime" + } + ] } + }, "parameters": { "id": { @@ -285,6 +338,14 @@ "required": true, "type": "integer", "format": "int64" + }, + "itemId": { + "name": "itemId", + "in": "path", + "description": "Item id", + "required": true, + "type": "integer", + "format": "int64" } }, "securityDefinitions": { diff --git a/test/fixtures/extensions/items.js b/test/fixtures/extensions/items.js new file mode 100644 index 0000000..c750ba2 --- /dev/null +++ b/test/fixtures/extensions/items.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = function handler(req, res) { + +}; diff --git a/test/test-routebuilder.js b/test/test-routebuilder.js index e70eacc..fa5a258 100644 --- a/test/test-routebuilder.js +++ b/test/test-routebuilder.js @@ -12,7 +12,7 @@ test('routebuilder', function (t) { t.test('build directory', function (t) { routes = buildroutes({ api: api, basedir: path.join(__dirname, 'fixtures'), handlers: path.join(__dirname, 'fixtures/handlers')}); - t.strictEqual(routes.length, 4, 'added 4 routes.'); + t.strictEqual(routes.length, 5, 'added 5 routes.'); routes.forEach(function (route) { t.ok(route.hasOwnProperty('method'), 'has method property.'); @@ -36,7 +36,7 @@ test('routebuilder', function (t) { t.test('build from x-handler', function (t) { routes = buildroutes({ api: api, basedir: path.join(__dirname, 'fixtures')}); - t.strictEqual(routes.length, 2, 'added 2 routes.'); + t.strictEqual(routes.length, 3, 'added 3 routes.'); routes.forEach(function (route) { t.ok(route.hasOwnProperty('method'), 'has method property.'); @@ -73,7 +73,7 @@ test('routebuilder', function (t) { schemaValidator: schemaValidator }); - t.strictEqual(routes.length, 6, 'added 6 routes.'); + t.strictEqual(routes.length, 7, 'added 7 routes.'); routes.forEach(function (route) { t.ok(route.hasOwnProperty('method'), 'has method property.'); @@ -119,13 +119,15 @@ test('routebuilder', function (t) { t.test('route validator merge', function(t) { var route; route = routes[5]; - t.strictEqual(route.validators.length, 3, 'has 3 validators.'); var validator; validator = route.validators.filter(function (validator) {return validator.parameter.name === 'date'}).shift(); t.ok(validator.parameter.required, 'override by operation.'); + route = routes[6]; + t.strictEqual(route.validators.length, 4, 'has 4 validators.'); + t.end(); }); diff --git a/test/test-swaggerize.js b/test/test-swaggerize.js index 2c338be..ae0bcfc 100644 --- a/test/test-swaggerize.js +++ b/test/test-swaggerize.js @@ -43,7 +43,7 @@ test('configure', function (t) { }); t.ok(thing.isArray(routes), 'returns array.'); - t.strictEqual(routes.length, 4, 'routes.length 4.'); + t.strictEqual(routes.length, 5, 'routes.length 5.'); }); }); From 409ab11c1dd793650d6f54d55dfcfaaf3d863d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=A4=91=ED=9B=88?= Date: Wed, 22 Jun 2016 03:17:44 +0900 Subject: [PATCH 2/2] removed comments --- lib/buildroutes.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/buildroutes.js b/lib/buildroutes.js index ad1ef9d..e030784 100644 --- a/lib/buildroutes.js +++ b/lib/buildroutes.js @@ -49,19 +49,6 @@ function buildroutes(options) { }; validators = [].concat ( def.parameters || [], operation.parameters || [] ); - // validators = {}; - // if (def.parameters) { - // def.parameters.forEach(function (parameter) { - // validators[parameter.in + parameter.name] = parameter; - // }); - // } - // - // if (operation.parameters) { - // operation.parameters.forEach(function (parameter) { - // validators[parameter.in + parameter.name] = parameter; - // }); - // } - route.validators = validator.makeAll(validators, route); pathnames = [];