Repository navigation
Conversation
…e same $id
When the schema being compiled had an $id that was also among the external
schemas, responseSchemaCompiler removed it by replacing fjsOpts.schema. That
fjsOpts object is bound once per factory call and shared by every later
compilation, so the schema stayed missing afterwards and any later
{ $ref: '<id>#' } failed with "Cannot resolve ref".
Fastify reuses one serializer compiler for all routes of an encapsulated
context that has no addSchema of its own. A route using
fastify.getSchema('user') as a response schema therefore broke every later
route in that plugin that referenced 'user#', depending only on route order.
The duplicate $id is now removed from a per-call copy of the options.
npm test: 11/11 passing, 100% coverage, tstyche 24/24. npm run lint: clean.
The new test fails on main with "Schema with id "schema1" is not found".
Signed-off-by: DenisCDev <coldgreen04@gmail.com>
SameRainbows
reviewed
Nov 1, 2026
SameRainbows
left a comment
There was a problem hiding this comment.
Validated this locally. Reassigning fjsOpts to a shallow copy with a cloned schema map cleanly prevents the delete mutation from mutating the bound options across subsequent route compilations.
The reproduction scenario with plugin contexts (child.get('/a', ...) followed by child.get('/b', ...) with $ref) is verified, and the test in test/duplicate-schema.test.js exercises the exact sequence without adding overhead.
Ready to merge from my testing.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
responseSchemaCompilerdrops the schema being compiled from the external schemas when it has the same$id(#14). It does that by replacingfjsOpts.schema, butfjsOptsis bound once inbuildSerializerFactoryand shared by every later call. After the first compile the schema is gone for good, and a later{ $ref: '<id>#' }fails withCannot resolve ref.In Fastify this shows up inside a plugin that has no
addSchemaof its own, because that context reuses one serializer compiler for all its routes:It works if the two routes are swapped, or if they are registered on the root instance (the root has its own
addSchema, so it builds a new compiler per route). Reproduced on fastify 5.12.7 and on fastify main.The fix removes the
$idfrom a per-call copy of the options, so the behavior from #14 is kept for each compile and the shared options are left untouched.Checklist
npm run test && npm run benchmark --if-presentand the Code of conduct
The new test in
test/duplicate-schema.test.jsfails on main withSchema with id "schema1" is not foundand passes here.npm test(11/11, 100% coverage, tstyche) andnpm run lintpass. I also ran fastify's schema and internals tests (test/schema-*.test.js,test/output-validation.test.jsandtest/internals/*.test.js, 25 files) against fastify main with this compiler in place: 2572 passed, 0 failed, 6 skipped.