Skip to content

Commit e483299

Browse files
author
Manor
committed
added fix and test for non exist path in swagger
1 parent e81d9c4 commit e483299

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/inputValidationError.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @extends {Error}
88
*/
99
class InputValidationError extends Error {
10-
constructor(errors, path, method, options) {
10+
constructor(errors, path, method, options = {}) {
1111
super('Input validation error');
1212

1313
if (options.beautifyErrors && options.firstError) {

src/middleware.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function _validateRequest(requestOptions) {
9595

9696
function _validateBody(body, path, method) {
9797
return new Promise(function (resolve, reject) {
98-
if (schemas[path][method].body && !schemas[path][method].body.validate(body)) {
98+
if (schemas[path] && schemas[path][method] && schemas[path][method].body && !schemas[path][method].body.validate(body)) {
9999
return reject(schemas[path][method].body.errors);
100100
}
101101
return resolve();
@@ -104,7 +104,7 @@ function _validateBody(body, path, method) {
104104

105105
function _validateParams(headers, pathParams, query, files, path, method) {
106106
return new Promise(function (resolve, reject) {
107-
if (schemas[path][method].parameters && !schemas[path][method].parameters({ query: query, headers: headers, path: pathParams, files: files })) {
107+
if (schemas[path] && schemas[path][method] && schemas[path][method].parameters && !schemas[path][method].parameters({ query: query, headers: headers, path: pathParams, files: files })) {
108108
return reject(schemas[path][method].parameters.errors);
109109
}
110110

test/openapi3/openapi3-test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,20 @@ describe('input-validation middleware tests', function () {
118118
done();
119119
});
120120
});
121-
121+
it('when path does not exist in swagger - should not execute validation on request', function (done) {
122+
request(app)
123+
.post('/non-exist-path-in-swagger')
124+
.send({})
125+
.expect(200, function (err, res) {
126+
if (err) {
127+
throw err;
128+
}
129+
expect(res.body).to.eql({
130+
'result': 'OK'
131+
});
132+
done();
133+
});
134+
});
122135
describe('discriminator-pet', function () {
123136
it('missing discriminator field', function (done) {
124137
request(app)

test/openapi3/test-server-pet.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ module.exports = function (options) {
3939
app.post('/pet-discriminator-on-child', inputValidation.validate, function (req, res, next) {
4040
res.json({ result: 'OK' });
4141
});
42+
app.post('/non-exist-path-in-swagger',inputValidation.validate, function (req, res, next) {
43+
res.json({ result: 'OK' });
44+
});
4245

4346
app.use(function (err, req, res, next) {
4447
if (err instanceof inputValidation.InputValidationError) {

0 commit comments

Comments
 (0)