Skip to content

Commit f3ae591

Browse files
nicolaemarinNicolae Marin
andauthored
fix: match route with empty path parameters for api with child resource (#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 <[email protected]>
1 parent afb861b commit f3ae591

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

src/utils/schemaEndpointResolver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function _pathMatcherInternal(routes, path, exactMatch) {
3131

3232
if (!exactMatch) {
3333
// if current path segment is param
34-
if (seg.startsWith(':') && pathArr[idx]) return true;
34+
if (seg.startsWith(':') && (idx in pathArr)) return true;
3535
}
3636

3737
return false;

test/fastify/fastify-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ describe('fastify plugin', () => {
3030
reply.status(204).send();
3131
});
3232

33+
app.get('/pets/:petId/medicalHistory', (req, reply) => {
34+
reply.status(204).send();
35+
});
36+
3337
app.post('/pets', (req, reply) => {
3438
reply.status(201).send();
3539
});
@@ -106,4 +110,22 @@ describe('fastify plugin', () => {
106110
}).post('/pets');
107111
expect(response.statusCode).to.equal(400);
108112
});
113+
it('Invalid path parameter - too short', async () => {
114+
const response = await app.inject()
115+
.headers({
116+
'api-version': '1.0'
117+
})
118+
.get('/pets/11/medicalHistory');
119+
expect(response.statusCode).to.equal(400);
120+
console.log(response);
121+
});
122+
it('Invalid path parameter - empty', async () => {
123+
const response = await app.inject()
124+
.headers({
125+
'api-version': '1.0'
126+
})
127+
.get('/pets//medicalHistory');
128+
expect(response.statusCode).to.equal(400);
129+
console.log(response);
130+
});
109131
});

test/pet-store-swagger.yaml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,68 @@ paths:
226226
description: unexpected error
227227
schema:
228228
$ref: '#/definitions/Error'
229+
/pets/{petId}/medicalHistory:
230+
get:
231+
summary: Medical history for a specific pet
232+
operationId: medicalHistoryByPetId
233+
tags:
234+
- pets
235+
parameters:
236+
- $ref: '#/parameters/ApiVersion'
237+
- $ref: '#/parameters/ApiRequestId'
238+
- name: petId
239+
in: path
240+
required: true
241+
description: The id of the pet to retrieve
242+
type: string
243+
minLength: 3
244+
maxLength: 10
245+
responses:
246+
"200":
247+
description: Expected response to a valid request
248+
schema:
249+
$ref: '#/definitions/Pets'
250+
default:
251+
description: unexpected error
252+
schema:
253+
$ref: '#/definitions/Error'
254+
put:
255+
summary: Medical history for a specific pet
256+
operationId: medicalHistoryByPetId
257+
tags:
258+
- pets
259+
consumes:
260+
- application/json
261+
parameters:
262+
- $ref: '#/parameters/ApiVersion'
263+
- $ref: '#/parameters/ApiRequestId'
264+
- name: petId
265+
in: path
266+
required: true
267+
description: The id of the pet to retrieve
268+
type: string
269+
minLength: 3
270+
maxLength: 10
271+
- name: body
272+
in: body
273+
schema:
274+
type: object
275+
properties:
276+
name:
277+
type: string
278+
age:
279+
type: integer
280+
tag:
281+
type: string
282+
responses:
283+
"200":
284+
description: Expected response to a valid request
285+
schema:
286+
$ref: '#/definitions/Pets'
287+
default:
288+
description: unexpected error
289+
schema:
290+
$ref: '#/definitions/Error'
229291
/pets/search:
230292
get:
231293
summary: Search for a pet

0 commit comments

Comments
 (0)