Skip to content

Commit 2ee54b6

Browse files
msmolkibertoad
authored andcommitted
Fix pathMatcher to prioritize exact matches (#101)
1 parent 4581736 commit 2ee54b6

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

src/utils/schemaEndpointResolver.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function getMethodSchemaInternal(schemas, path, method) {
1717
}
1818
}
1919

20-
function pathMatcher(routes, path) {
20+
function _pathMatcherInternal(routes, path, exactMatch) {
2121
return Object
2222
.keys(routes)
2323
.find((route) => {
@@ -29,12 +29,18 @@ function pathMatcher(routes, path) {
2929
return routeArr.every((seg, idx) => {
3030
if (seg === pathArr[idx]) return true;
3131

32-
// if current path segment is param
33-
if (seg.startsWith(':') && pathArr[idx]) return true;
32+
if (!exactMatch) {
33+
// if current path segment is param
34+
if (seg.startsWith(':') && pathArr[idx]) return true;
35+
}
3436

3537
return false;
3638
});
3739
});
3840
}
3941

42+
function pathMatcher(routes, path) {
43+
return _pathMatcherInternal(routes, path, true) || _pathMatcherInternal(routes, path, false);
44+
}
45+
4046
module.exports = SchemaEndpointResolver;

test/express/middleware-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ describe('input-validation middleware tests - Express', function () {
3939
done();
4040
});
4141
});
42+
it('valid request - should validate using correct schema - path exact match', function (done) {
43+
request(app)
44+
.get('/pets/search')
45+
.set('api-version', '1.0')
46+
.set('request-id', '123456')
47+
.query({ terms: 'foobar' })
48+
.expect(200, function (err, res) {
49+
if (err) {
50+
throw err;
51+
}
52+
expect(res.body.result).to.equal('OK');
53+
done();
54+
});
55+
});
56+
it('valid request - should validate using correct schema - path partial match', function (done) {
57+
request(app)
58+
.get('/pets/123')
59+
.set('api-version', '1.0')
60+
.set('request-id', '123456')
61+
.expect(200, function (err, res) {
62+
if (err) {
63+
throw err;
64+
}
65+
expect(res.body.result).to.equal('OK');
66+
done();
67+
});
68+
});
4269
it('missing header - should fail', function (done) {
4370
request(app)
4471
.get('/pets')

test/express/test-simple-server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module.exports = () => {
1515
app.post('/pets', inputValidation.validate, function (req, res, next) {
1616
res.json({ result: 'OK' });
1717
});
18+
app.get('/pets/search', inputValidation.validate, function(req, res, next) {
19+
res.json({ result: 'OK' });
20+
});
1821
app.get('/pets/:petId', inputValidation.validate, function (req, res, next) {
1922
res.json({ result: 'OK' });
2023
});

test/pet-store-swagger.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,29 @@ paths:
226226
description: unexpected error
227227
schema:
228228
$ref: '#/definitions/Error'
229+
/pets/search:
230+
get:
231+
summary: Search for a pet
232+
operationId: searchPets
233+
tags:
234+
- pets
235+
parameters:
236+
- $ref: '#/parameters/ApiVersion'
237+
- $ref: '#/parameters/ApiRequestId'
238+
- name: terms
239+
in: query
240+
required: true
241+
description: The search term to match pets
242+
type: string
243+
responses:
244+
"200":
245+
description: Expected response to a valid request
246+
schema:
247+
$ref: '#/definitions/Pets'
248+
default:
249+
description: unexpected error
250+
schema:
251+
$ref: '#/definitions/Error'
229252
/heartbeat:
230253
get:
231254
summary: Info for current system status

0 commit comments

Comments
 (0)