Repository navigation
fix(fastify): send object error bodies with a json content type - #18253
Merged
kamilmysliwiec merged 2 commits intoOct 24, 2026
Merged
kamilmysliwiec merged 2 commits into
kamilmysliwiec merged 2 commits into
Conversation
FastifyAdapter.reply() replaced a non-JSON Content-Type set on the route (e.g. with @Header() or in middleware) only when the body had a statusCode of 400 or more. A custom HttpException body, as in new NotFoundException({ code, message }), need not have one. Fastify only serializes an object when the Content-Type is JSON or not set, so it threw FST_ERR_REP_INVALID_PAYLOAD_TYPE and answered 500: the exception's status and body were lost. An object body sent with an error status code now gets the JSON Content-Type too, as on Express since 7dc6e34. Streams and binary bodies, which Fastify sends as they are, keep the route's type, and so do text bodies. Behaviour change: on a route with a non-JSON Content-Type, such a response is labelled application/json and logs the same warning as error bodies with a statusCode already did.
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?
A route sets
Content-Type: text/csv, then throwsnew NotFoundException({ code, message }).reply()only switches toapplication/jsonwhenbody.statusCode >= 400(fastify-adapter.ts#L767-L779), and a custom body has nostatusCode, so the csv type stays. Fastify won't serialize a plain object under a non-JSON type without a reply serializer, so it throwsFST_ERR_REP_INVALID_PAYLOAD_TYPEand the client gets a 500 in place of the 404.Real HTTP on master, fastify 5.12.7. The built-in body has a
statusCodeand gets through, express answers the custom one fine:Issue Number: N/A
Raw test output with
fastify-adapter.tsfrom master:What is the new behavior?
reply()also forcesapplication/jsonfor an object body that isn't a stream (.pipe) or anArrayBufferview (Buffer, TypedArray) when thestatusCodeargument is 400 or more, the rule 7dc6e34 added to Express. It lives in anisErrorBody()helper next toisJsonContentTypesoreply()doesn't get more branches.Text, Buffer and stream error bodies keep the route's type. Bodies with a
statusCode, JSON and+jsonroutes, and successful object bodies behave as before.Does this PR introduce a breaking change?
Behaviour change, as in 7dc6e34: on a non-JSON route such an error reply is now labelled
application/jsonand logs the existing "Content-Type doesn't match Reply body" warning. With a custom reply serializer, master sent it under the route's type.Other information
Same condition as Express after 7dc6e34, and it extends the
statusCodeswitch from #10474 (for #8469) to custom error bodies.#18208 was a successful object reply on a non-JSON route, kept as plain Fastify does it, and this doesn't change it (
/ok.csvstill answers 500 on the branch). It only touches error replies, where the 500 hides the exception Nest is sending.The 2 tests above fail on master and pass here; the related specs pass (166 tests), and prettier and oxlint are clean.