Skip to content
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
5 changes: 5 additions & 0 deletions src/frameworks/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ function getValidator(validateRequest) {

function _getParameters(req) {
const requestOptions = {};
if (!req.route || !req.route.path) {
req.route = {
path: req.path
};
}
const path = req.baseUrl.concat(req.route.path);
requestOptions.path = path.endsWith('/') ? path.substring(0, path.length - 1) : path;
requestOptions.headers = req.headers;
Expand Down
14 changes: 14 additions & 0 deletions test/express/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,20 @@ describe('input-validation middleware tests - Express', function () {
done();
});
});
it('valid request - use with router module', function (done) {
request(app)
.get('/petsRouter/pets/1234')
.set('api-version', '1.0')
.set('request-id', '123456')
.query({ page: 0 })
.expect(200, function (err, res) {
if (err) {
throw err;
}
expect(res.body.result).to.equal('OK');
done();
});
});
});
describe('Simple server - type coercion enabled', function () {
let app;
Expand Down
2 changes: 2 additions & 0 deletions test/express/test-simple-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const express = require('express');
const bodyParser = require('body-parser');
const inputValidation = require('../../src/middleware');
const router = require('../router');

module.exports = () => {
inputValidation.init('test/pet-store-swagger.yaml');
Expand All @@ -24,6 +25,7 @@ module.exports = () => {
app.put('/pets', inputValidation.validate, function (req, res, next) {
res.json({ result: 'OK' });
});
app.use('/petsRouter', inputValidation.validate, router);
app.use(function (err, req, res, next) {
if (err instanceof inputValidation.InputValidationError) {
res.status(400).json({ more_info: JSON.stringify(err.errors) });
Expand Down