-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
🐛 Bug Report
Setting ignoreTrailingSlash: true on main Fastify instance and registering a plugin with tralingSlash prefixed path (prefix: '/attendances/'), prevents avvio from crashing on startup with AssertionError [ERR_ASSERTION]: Method 'GET' already declared for route '/v1/attendances/' error.
As you can see on repro repo, in this case fastify registers two routes on the inner plugin and starts the server:
- GET /v1/attendances/
- GET /v1/attendances//
After the server is ready, calling /v1/attendances// route will result in TypeError: Cannot read property 'length' of undefined in /lib/hooks.js:65:32.
This happens because /lib/route.js#L246 is never reached for /v1/attendances// and therefore some pre* hooks (preParsing, preValidation preSerialization) are never set to null. There is only a default for preHandler hook lib/context.js#L16. The others are supposed to be set in afterRouteAdded function, but for /v1/attendances// the execution goes in lib/route.js#L228 (as already pointed out with anAssertionError [ERR_ASSERTION]: Method 'GET' already declared for route '/v1/attendances/').
The done callback is located in avvio plugin.js#L98 and when called for /v1/attendances// it finds the completed===true because of /v1/attendances/. The bug is generated on lib/route.js#L144 since it calls afterRouteAdded.call two times in a row. In fact, using a non default value for prefixTrailingSlash (so slash or no-slash) it works as expected.
To Reproduce
Clone this repo:
npm i
npm run dev
call the /v1/attendances// rout using postman or similar.
Expected behavior
Avvio must stop loading the plugin and throwing AssertionError [ERR_ASSERTION]: Method 'GET' already declared for route '/v1/attendances/' error.
Your Environment
- node version: 10
- fastify version: >=2.4.1
- os: Mac