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: match route with empty path parameters for api with child resource #178

Merged
merged 6 commits into from
Feb 28, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/utils/schemaEndpointResolver.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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