Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix missing validators for referencing parameters #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions lib/buildroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,7 @@ 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 || [] );
route.validators = validator.makeAll(validators, route);

pathnames = [];
Expand Down
15 changes: 10 additions & 5 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
},

Expand Down Expand Up @@ -100,7 +105,7 @@ module.exports = function validator(options) {
}

if (parameter.in !== 'body' && parameter.allowEmptyValue){
schema = schema.allow('').optional();
schema = schema.allow('').optional();
}

return {
Expand Down
61 changes: 61 additions & 0 deletions test/fixtures/defs/pets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/extensions/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function handler(req, res) {

};
10 changes: 6 additions & 4 deletions test/test-routebuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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.');
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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();
});

Expand Down
2 changes: 1 addition & 1 deletion test/test-swaggerize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});

});
Expand Down