diff --git a/src/utils/schemaEndpointResolver.js b/src/utils/schemaEndpointResolver.js index 17b4448..68068fb 100644 --- a/src/utils/schemaEndpointResolver.js +++ b/src/utils/schemaEndpointResolver.js @@ -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; diff --git a/test/fastify/fastify-test.js b/test/fastify/fastify-test.js index 348314f..e3576a3 100644 --- a/test/fastify/fastify-test.js +++ b/test/fastify/fastify-test.js @@ -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(); }); @@ -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); + }); }); diff --git a/test/pet-store-swagger.yaml b/test/pet-store-swagger.yaml index fe8fe91..5883ef5 100644 --- a/test/pet-store-swagger.yaml +++ b/test/pet-store-swagger.yaml @@ -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