compileSerializationSchema reuses the first serializer when metadata changes
Description
reply.compileSerializationSchema() forwards httpStatus and contentType to a custom serializer compiler, but its cache is keyed only by the schema object. Reusing the same schema object with different metadata therefore returns the serializer compiled for the first call.
This is present on main at 1beaf7e72d24b2fc63a02a7f5806772a00e45454:
compileSerializationSchemachecks and stores by schema identity only- The documentation says both optional values are forwarded to a custom compiler
Reproduction
const Fastify = require('fastify')
const app = Fastify()
const compiled = []
app.setSerializerCompiler(({ httpStatus, contentType }) => {
compiled.push({ httpStatus, contentType })
return (data) => JSON.stringify({ httpStatus, contentType, data })
})
app.get('/', (_request, reply) => {
const schema = { type: 'object' }
const first = reply.compileSerializationSchema(schema, '200', 'application/json')
const second = reply.compileSerializationSchema(schema, '201', 'application/vnd.example+json')
return {
sameFunction: first === second,
compiled,
first: first({ value: 1 }),
second: second({ value: 2 }),
}
})
app.inject({ method: 'GET', url: '/' }).then(({ payload }) => console.log(payload))
Actual behavior
The compiler is invoked once, sameFunction is true, and both calls use the 200 / application/json metadata captured by the first serializer.
Expected behavior
Changing httpStatus or contentType should produce (or retrieve) a serializer compiled for that metadata tuple.
Possible direction
Keep the weak schema identity cache, but store serializers below it by httpStatus and contentType, including a stable representation for omitted values.
I found this while benchmarking an automated code-review workflow and manually verified the source path and reproduction before reporting it.
I had a look at fixing this and ran into a decision I do not think I should make on my own.
The cache is read in three places, not one:
compileSerializationSchema(lib/reply.js L378)serializeInput(L437), which has the same problem, it checks the cache by schema before falling through to compilegetSerializationFunction(L367), which returnskReplyCacheSerializeFns.get(schemaOrStatus)straight to the caller
That third one is the awkward part. It is public API and it hands the cached value back as the serializer, so if the cache value becomes a map of metadata to function, getSerializationFunction(schema) would start returning a map instead of a function unless it is updated too.
Updating it needs a decision about what it should return once a schema has been compiled under several different metadata combinations. Today it returns whichever one was compiled first, which is the bug being reported here, so there is no existing behaviour worth preserving exactly. The options I can see are to return the one compiled with no metadata, or to use its existing second contentType argument for the lookup, which is currently ignored on the object branch.
Happy to send a PR for whichever you prefer, or for something else if I have misread the intent. I did not want to guess at public API semantics and waste your review time.
serializeInput looks like it can just delegate to compileSerializationSchema rather than repeating the lookup, since that method already consults the cache itself.