Skip to content

Commit

Permalink
fix: match route with empty path parameters for api with child resour…
Browse files Browse the repository at this point in the history
…ce (#178)

* Update schemaEndpointResolver.js

Check only if the key exists in path attributes when matching the route without exactMatch for cases when path parameter is empty

* Update middleware-test.js

Add test for case when path parameter is empty string

* Update middleware-test.js

remove express test case

* Update pet-store-swagger.yaml

define swagger path for child resource

* Update fastify-test.js

add tests for api with child resource and empty path parameter

* lint fix

Co-authored-by: Nicolae Marin <nicolae.marin@payu.ro>
  • Loading branch information
nicolaemarin and Nicolae Marin committed Feb 28, 2022
1 parent afb861b commit f3ae591
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/schemaEndpointResolver.js
Expand Up @@ -31,7 +31,7 @@ function _pathMatcherInternal(routes, path, exactMatch) {

if (!exactMatch) {
// if current path segment is param
if (seg.startsWith(':') && pathArr[idx]) return true;
if (seg.startsWith(':') && (idx in pathArr)) return true;
}

return false;
Expand Down
22 changes: 22 additions & 0 deletions test/fastify/fastify-test.js
Expand Up @@ -30,6 +30,10 @@ describe('fastify plugin', () => {
reply.status(204).send();
});

app.get('/pets/:petId/medicalHistory', (req, reply) => {
reply.status(204).send();
});

app.post('/pets', (req, reply) => {
reply.status(201).send();
});
Expand Down Expand Up @@ -106,4 +110,22 @@ describe('fastify plugin', () => {
}).post('/pets');
expect(response.statusCode).to.equal(400);
});
it('Invalid path parameter - too short', async () => {
const response = await app.inject()
.headers({
'api-version': '1.0'
})
.get('/pets/11/medicalHistory');
expect(response.statusCode).to.equal(400);
console.log(response);
});
it('Invalid path parameter - empty', async () => {
const response = await app.inject()
.headers({
'api-version': '1.0'
})
.get('/pets//medicalHistory');
expect(response.statusCode).to.equal(400);
console.log(response);
});
});
62 changes: 62 additions & 0 deletions test/pet-store-swagger.yaml
Expand Up @@ -226,6 +226,68 @@ paths:
description: unexpected error
schema:
$ref: '#/definitions/Error'
/pets/{petId}/medicalHistory:
get:
summary: Medical history for a specific pet
operationId: medicalHistoryByPetId
tags:
- pets
parameters:
- $ref: '#/parameters/ApiVersion'
- $ref: '#/parameters/ApiRequestId'
- name: petId
in: path
required: true
description: The id of the pet to retrieve
type: string
minLength: 3
maxLength: 10
responses:
"200":
description: Expected response to a valid request
schema:
$ref: '#/definitions/Pets'
default:
description: unexpected error
schema:
$ref: '#/definitions/Error'
put:
summary: Medical history for a specific pet
operationId: medicalHistoryByPetId
tags:
- pets
consumes:
- application/json
parameters:
- $ref: '#/parameters/ApiVersion'
- $ref: '#/parameters/ApiRequestId'
- name: petId
in: path
required: true
description: The id of the pet to retrieve
type: string
minLength: 3
maxLength: 10
- name: body
in: body
schema:
type: object
properties:
name:
type: string
age:
type: integer
tag:
type: string
responses:
"200":
description: Expected response to a valid request
schema:
$ref: '#/definitions/Pets'
default:
description: unexpected error
schema:
$ref: '#/definitions/Error'
/pets/search:
get:
summary: Search for a pet
Expand Down

0 comments on commit f3ae591

Please sign in to comment.