Repository navigation
fix(fastify): treat a JSON content type with parameters as JSON in reply() - #17867
Merged
kamilmysliwiec merged 1 commit intoSep 24, 2026
Merged
Conversation
…ply()
reply() compared the Content-Type header with the exact string
"application/json", so a value such as "application/json; charset=utf-8"
was treated as a non-JSON content type: the adapter logged the
"Content-Type doesn't match Reply body" warning for a JSON body and then
overwrote the header, dropping the charset parameter.
Compare with startsWith("application/json") instead, which is what the
express adapter has done since the same check was fixed there.
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.
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
FastifyAdapter.reply()compares theContent-Typeheader with the exact stringapplication/json. A JSON content type that carries parameters, such asapplication/json; charset=utf-8, is therefore treated as non-JSON when an error body is sent: the adapter logsfor a body that is JSON, and then overwrites the header with
application/json, dropping the charset.This is easy to hit because
@Header()values are applied before pipes run, so a handler likelogs the warning on every
GET /abc.The express adapter had the same check and was changed to
startsWith('application/json')in 4f391c2; the fastify adapter was not updated, so the two adapters behave differently.What is the new behavior?
reply()checkstypeof contentType === 'string' && !contentType.startsWith('application/json'), matching the express adapter. Parameterised JSON content types are left alone; non-JSON content types are still replaced byapplication/jsonfor error bodies.Two unit tests cover both cases in
fastify-adapter.spec.ts.Does this PR introduce a breaking change?
Other information