Repository navigation
fix(fastify): derive neutral version when accept header is absent - #17705
kamilmysliwiec merged 2 commits into
Conversation
With MEDIA_TYPE versioning, a request without an Accept header initialized the version parameter to '' instead of undefined, so the isUndefined guard added in nestjs#9585 (whose comment says 'No version was supplied') never fired. The derived constraint became undefined and find-my-way skipped routes registered with [VERSION_NEUTRAL, ...] arrays, returning 404. The HEADER branch of the same function, the present-but-versionless Accept sub-case, and the Express adapter all resolve this case to VERSION_NEUTRAL. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
micalevisk
left a comment
There was a problem hiding this comment.
Thanks for the PR, the fix itself is correct and I've verified it locally:
- reverted the one-liner and the new absent-header test fails with 404 vs 200, the two control cases keep passing
- all platform-fastify unit tests pass, and the whole
integration/versioningsuite (555 tests) passes with the change, including the existingAccept: ''→ 404 case - the
#9585attribution in the commit body is accurate
Requesting a few changes on the test side before merging:
1. Remove the any casts
Both casts are unnecessary. VersionValue already accepts Array<string | typeof VERSION_NEUTRAL>, and the options object fits VersioningOptions directly. I tried this locally and the file type-checks and passes unchanged:
import { VERSION_NEUTRAL, VersioningOptions, VersioningType } from '@nestjs/common';
const versioningOptions: VersioningOptions =
type === VersioningType.MEDIA_TYPE
? { type, key: 'v=' }
: { type, header: 'X-API-Version' };
const versionedHandler = fastifyAdapter.applyVersionFilter(
handler,
[VERSION_NEUTRAL, '2'],
versioningOptions,
);2. Fold the cases into fastify-adapter.spec.ts
This package keeps one spec per adapter. Please move the three cases into the existing packages/platform-fastify/test/adapters/fastify-adapter.spec.ts as a describe('versionConstraint') (or describe('applyVersionFilter')) block instead of a new file, and use beforeEach for the adapter creation like the rest of that spec does rather than constructing it inside each it.
Also drop the (repro) suffix from the describe title: this is a permanent regression test, not a reproduction.
3. Add integration coverage for the array-neutral case
None of the controllers under integration/versioning/src use a VERSION_NEUTRAL array, which is exactly why the existing GET /neutral → "No Header" test never caught this (scalar VERSION_NEUTRAL routes are registered without a find-my-way constraint, arrays are constrained). Please add a controller/route using @Version([VERSION_NEUTRAL, '2']) (or similar) and a "No Header" case in both media-type-versioning.spec.ts and media-type-versioning-fastify.spec.ts, so this is guarded at the framework level for both adapters.
4. Mention the empty-header side effect in the description (nit)
Since the empty string is falsy, a present-but-empty Accept: '' header now also derives VERSION_NEUTRAL. That already matches the Express path in router-explorer.ts and no existing test depends on the old behavior, but it's a second observable change and the PR description only talks about the absent-header case.
FYI: tsc -p tsconfig.spec.json reports a TS2835 (missing .js extension) on the spec's relative import, but the two existing fastify specs have the identical error, so that's pre-existing and not something you need to address here.
Fold the standalone media-type repro spec into fastify-adapter.spec.ts as an applyVersionFilter describe block, dropping both any casts by typing the options as VersioningOptions and passing the version array directly (VersionValue accepts Array<string | typeof VERSION_NEUTRAL>). Add integration coverage: a route versioned [VERSION_NEUTRAL, '2'] plus a No Header case in both media-type versioning e2e suites. The express suite passes on the current adapter; the fastify case exercises the fix. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Thanks for the thorough review and for verifying the fix independently — all four points addressed in 9eb04e2:
|
micalevisk
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. I re-verified 9eb04e2 locally and all four points from the previous round are addressed:
- Both
anycasts are gone; the spec type-checks withVersioningOptionsand the typedFastifyRequest/FastifyReplyhandler. - The cases now live in
fastify-adapter.spec.tsunderdescribe('applyVersionFilter'), using the file's outerbeforeEach, and the standalone spec is removed. MultipleVersionNeutralControllerplus theGET /multiple-neutralblock in both e2e suites is exactly the framework-level guard that was missing. With the one-liner reverted, the only failure across the wholeintegration/versioningdirectory is the fastifyNo Headercase (404 vs 200); the Express suite stays green.- The
Accept: ''side effect is documented.
Results on the PR head:
- platform-fastify unit tests: 20 passed
integration/versioning(all 9 files): 563 passed- oxlint, prettier,
tsc -b packages/platform-fastify: clean
One last thing before I approve, description only:
PR description still references the deleted spec
The "What is the new behavior?" section says:
new spec
media-type-version-neutral.spec.tscovers the absent-header case (fails before the fix with 404 vs 200) plus the two control cases. All 20 platform-fastify tests pass.
That file no longer exists after 9eb04e2. Please update the sentence to point to the applyVersionFilter block in packages/platform-fastify/test/adapters/fastify-adapter.spec.ts and mention the new GET /multiple-neutral e2e coverage in both media-type suites. No code change needed; I'll approve once the description matches the diff.
|
Description updated — it now points to the |
|
lgtm |
| datasource | package | from | to | | ---------- | ------------------------ | ------ | ------ | | npm | @nestjs/common | 12.0.1 | 12.1.0 | | npm | @nestjs/core | 12.0.1 | 12.1.0 | | npm | @nestjs/platform-express | 12.0.1 | 12.1.0 | ## [v12.1.0](https://github.com/nestjs/nest/releases/tag/v12.1.0) ##### v12.1.0 (2026-09-23) ##### Bug fixes - `core` - [#17838](nestjs/nest#17838) fix(core): inherit optional ctor params when the ctor is not redeclared ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17819](nestjs/nest#17819) fix(core): name the module when metadata entries are invalid ([@thekhegay](https://github.com/thekhegay)) - `microservices` - [#17828](nestjs/nest#17828) fix(microservices): let the redis client retry after a failed connect ([@hktitof](https://github.com/hktitof)) - [#17810](nestjs/nest#17810) fix(microservices): fail every pending request when a callback throws ([@hktitof](https://github.com/hktitof)) ##### Enhancements - `core` - [#17846](nestjs/nest#17846) fix(core): do not skip subsequent middleware after a transient one ([@lehafari](https://github.com/lehafari)) - [#17840](nestjs/nest#17840) fix(core): preserve route handler names ([@Grmiade](https://github.com/Grmiade)) - `common`, `core` - [#17713](nestjs/nest#17713) feat(core): add support for array of global prefixes ([@micalevisk](https://github.com/micalevisk)) - [#17834](nestjs/nest#17834) feat(common,core): add built-in, adapter-agnostic cookies ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - `platform-fastify` - [#17824](nestjs/nest#17824) fix(fastify): honor Nest forceCloseConnections in initHttpServer ([@xia-chao](https://github.com/xia-chao)) - `common` - [#17827](nestjs/nest#17827) fix(common): apply the ParseDatePipe default to every missing value ([@xia-chao](https://github.com/xia-chao)) - `platform-fastify`, `testing` - [#17835](nestjs/nest#17835) feat(fastify): add file upload interceptors backed by [@fastify/multipart](https://github.com/fastify/multipart) ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - `common`, `core`, `platform-fastify` - [#17836](nestjs/nest#17836) feat(common,core,fastify): built-in CSRF protection and security headers ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) ##### Committers: 9 - Abdellatif Anaflous ([@hktitof](https://github.com/hktitof)) - Alejandro ([@lehafari](https://github.com/lehafari)) - Chris Z ([@haimingZZ](https://github.com/haimingZZ)) - Johnny J. Huirilef Millán ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - Jérémy Fauvel ([@Grmiade](https://github.com/Grmiade)) - Kamil Mysliwiec ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - Micael Levi L. Cavalcante ([@micalevisk](https://github.com/micalevisk)) - Roman Khegay ([@thekhegay](https://github.com/thekhegay)) - Xia Chao ([@xia-chao](https://github.com/xia-chao)) ## [v12.0.4](https://github.com/nestjs/nest/releases/tag/v12.0.4) ##### v12.0.4 (2026-09-21) ##### Bug fixes - `microservices` - [#17816](nestjs/nest#17816) fix(microservices): let the kafka client retry after a failed connect ([@Xch4rt](https://github.com/Xch4rt)) - [#17786](nestjs/nest#17786) fix(microservices): fail pending rmq requests on client close ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - [#17802](nestjs/nest#17802) fix(microservices): fail pending nats requests on client close ([@hktitof](https://github.com/hktitof)) - [#17803](nestjs/nest#17803) fix(microservices): handle missing grpc package namespace gracefully ([@Se3do](https://github.com/Se3do)) - [#17799](nestjs/nest#17799) fix(microservices): share the processing hook envelope across transports ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17797](nestjs/nest#17797) fix(microservices): close the kafka request-response span exactly once ([@MingLin1995](https://github.com/MingLin1995)) - [#17794](nestjs/nest#17794) fix(microservices): close the request-response span exactly once ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17785](nestjs/nest#17785) fix(microservices): route message handler rejections to handleError ([@hktitof](https://github.com/hktitof)) - `core` - [#17821](nestjs/nest#17821) fix(core): hang on sparse factory inject array ([@xia-chao](https://github.com/xia-chao)) ##### Enhancements - `platform-express` - [#17818](nestjs/nest#17818) Fix: merge multer `limits` key-by-key instead of overriding it wholesale ([@mdrezuanislamridoy](https://github.com/mdrezuanislamridoy)) - `common` - [#17780](nestjs/nest#17780) fix(common): mark parse date pipe options as optional ([@Chy-Zaber-Bin-Zahid](https://github.com/Chy-Zaber-Bin-Zahid)) ##### Dependencies - `common` - [#17793](nestjs/nest#17793) fix(deps): update dependency file-type to v22.1.1 ([@renovate\[bot\]](https://github.com/apps/renovate)) - `platform-fastify` - [#17791](nestjs/nest#17791) fix(deps): update dependency fastify to v5.12.5 ([@renovate\[bot\]](https://github.com/apps/renovate)) ##### Committers: 10 - Abdellatif Anaflous ([@hktitof](https://github.com/hktitof)) - Chowdhury Zaber Bin Zahid ([@Chy-Zaber-Bin-Zahid](https://github.com/Chy-Zaber-Bin-Zahid)) - Imad - Prakticode ([@prakticode](https://github.com/prakticode)) - Johnny J. Huirilef Millán ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - Kamil Mysliwiec ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - MD. Ridoy Babu ([@mdrezuanislamridoy](https://github.com/mdrezuanislamridoy)) - Ming ([@MingLin1995](https://github.com/MingLin1995)) - Mohammed Said ([@Se3do](https://github.com/Se3do)) - Pablo Gutiérrez ([@Xch4rt](https://github.com/Xch4rt)) - Xia Chao ([@xia-chao](https://github.com/xia-chao)) ## [v12.0.3](https://github.com/nestjs/nest/releases/tag/v12.0.3) ##### v12.0.3 (2026-09-15) ##### Bug fixes - `microservices` - [#17766](nestjs/nest#17766) fix(microservices): always run the kafka processing end hook ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17757](nestjs/nest#17757) fix(microservices): fail pending requests on client close ([@hktitof](https://github.com/hktitof)) - [#17746](nestjs/nest#17746) fix(microservices): bound memory held for a TCP peer ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17745](nestjs/nest#17745) fix(microservices): destroy open TCP sockets on shutdown ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - `common` - [#17738](nestjs/nest#17738) fix(common): validate uuid version and variant when no version is set ([@otnc](https://github.com/otnc)) - [#17761](nestjs/nest#17761) fix(common): invoke proto-key stripping hook via this in transform ([@koreahghg](https://github.com/koreahghg)) ##### Dependencies - `platform-express` - [#17762](nestjs/nest#17762) fix(deps): update dependency multer to v2.4.0 ([@renovate\[bot\]](https://github.com/apps/renovate)) - `common` - [#17753](nestjs/nest#17753) fix(deps): update dependency file-type to v22.1.0 ([@renovate\[bot\]](https://github.com/apps/renovate)) ##### Committers: 4 - Abdellatif Anaflous ([@hktitof](https://github.com/hktitof)) - Kamil Mysliwiec ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - otoneko. ([@otnc](https://github.com/otnc)) - 한국 ([@koreahghg](https://github.com/koreahghg)) ## [v12.0.2](https://github.com/nestjs/nest/releases/tag/v12.0.2) ##### v12.0.2 (2026-09-14) ##### Bug fixes - `platform-socket.io`, `platform-ws` - [#17666](nestjs/nest#17666) fix(socket.io): keep the message stream alive when a handler throws ([@hktitof](https://github.com/hktitof)) - `microservices` - [#17728](nestjs/nest#17728) fix(microservices): detach signal listeners of connected microservices ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17671](nestjs/nest#17671) fix(microservices): fail requests whose reply subscription is rejected ([@samuelmbabhazi](https://github.com/samuelmbabhazi)) - [#17670](nestjs/nest#17670) fix(microservices): do not treat foreign JSON as an RPC envelope ([@xia-chao](https://github.com/xia-chao)) - [#17644](nestjs/nest#17644) fix(microservices): don't flatten plain handler results with pre-request hooks ([@iamolegga](https://github.com/iamolegga)) - [#17641](nestjs/nest#17641) fix(microservices): run the mqtt listen callback once ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - `common` - [#17710](nestjs/nest#17710) fix(common): collapse duplicate internal slashes in `normalizePath` ([@Asadshah7950](https://github.com/Asadshah7950)) - [#17655](nestjs/nest#17655) fix(common): validate UUID v3 variant ([@gwagjiug](https://github.com/gwagjiug)) - [#17702](nestjs/nest#17702) fix(common): treat empty string as array input in ParseArrayPipe ([@xia-chao](https://github.com/xia-chao)) - `core` - [#17667](nestjs/nest#17667) fix(core): discover factory providers with discovery decorators ([@hktitof](https://github.com/hktitof)) - [#17709](nestjs/nest#17709) fix(core): avoid treating error instances with `statusCode` as http errors ([@Asadshah7950](https://github.com/Asadshah7950)) - [#17625](nestjs/nest#17625) fix(core): clean up repeated shutdown signal listeners ([@Raj4478](https://github.com/Raj4478)) - `platform-fastify` - [#17705](nestjs/nest#17705) fix(fastify): derive neutral version when accept header is absent ([@Gaurav1112](https://github.com/Gaurav1112)) - [#17699](nestjs/nest#17699) fix(fastify): send responses for exceptions thrown from middleware ([@micalevisk](https://github.com/micalevisk)) ##### Enhancements - `microservices` - [#17734](nestjs/nest#17734) feat(microservices): add maxConnectionAttempts to ServerMqtt ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) ##### Dependencies - `platform-fastify` - [#17729](nestjs/nest#17729) fix(deps): update dependency fastify to v5.12.4 ([@renovate\[bot\]](https://github.com/apps/renovate)) ##### Committers: 16 - Abdellatif Anaflous ([@hktitof](https://github.com/hktitof)) - Alexey Solovyov ([@lyohaplotinka](https://github.com/lyohaplotinka)) - Asad Ali Shah ([@Asadshah7950](https://github.com/Asadshah7950)) - Gyeonghun Park ([@Gyeonghun-Park](https://github.com/Gyeonghun-Park)) - Johnny J. Huirilef Millán ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - Kamil Mysliwiec ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - Kumar Gaurav ([@Gaurav1112](https://github.com/Gaurav1112)) - Micael Levi L. Cavalcante ([@micalevisk](https://github.com/micalevisk)) - Ming ([@MingLin1995](https://github.com/MingLin1995)) - Mohammed Said ([@Se3do](https://github.com/Se3do)) - Rajeshwar Singh ([@Raj4478](https://github.com/Raj4478)) - Xia Chao ([@xia-chao](https://github.com/xia-chao)) - [@iamolegga](https://github.com/iamolegga) - cui fliter ([@cuishuang](https://github.com/cuishuang)) - samuel mbabhazi ([@samuelmbabhazi](https://github.com/samuelmbabhazi)) - 곽지욱 ([@gwagjiug](https://github.com/gwagjiug))
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
With
VersioningType.MEDIA_TYPEon the Fastify adapter, a request that sends noAcceptheader at all gets a 404 for routes registered with a version array containingVERSION_NEUTRAL(e.g.@Version([VERSION_NEUTRAL, '2'])).deriveConstraintinitializes the version parameter to''when the header is absent, so theisUndefined(...) ? VERSION_NEUTRALguard added in #9585 (whose own comment says "No version was supplied") never fires;''.split(key)[1]yieldsundefinedand find-my-way skips the constrained route. The asymmetry:Accept: application/json(present but versionless) correctly resolves toVERSION_NEUTRAL, absence of the header does not — and the HEADER versioning branch and the Express adapter both resolve absence toVERSION_NEUTRAL.What is the new behavior?
An absent
Acceptheader derivesVERSION_NEUTRAL, matching the same function's HEADER branch, its own present-but-versionless sub-case, and the Express adapter. One-line change; thedescribe('applyVersionFilter')block inpackages/platform-fastify/test/adapters/fastify-adapter.spec.tscovers the absent-header case (fails before the fix with 404 vs 200) plus the two control cases, andGET /multiple-neutral(@Version([VERSION_NEUTRAL, '2'])) adds framework-level e2e coverage in both media-type versioning suites — with the fix reverted, the fastifyNo Headercase is the only failure acrossintegration/versioning. All 20 platform-fastify unit tests and all 563integration/versioningtests pass.Second observable change: because the empty string is falsy, a present-but-empty
Accept: ''header now also derivesVERSION_NEUTRAL(previously it yieldedundefinedthe same way absence did). This matches the Express path inrouter-explorer.ts, and no existing test depends on the old behavior.Does this PR introduce a breaking change?
Other information
Prepared with the assistance of Claude (Anthropic); the failing/passing test runs and typecheck were verified by execution.