fix(zod-openapi): return 415 for request bodies with undeclared content type - #2087
Open
WolfieLeader wants to merge 1 commit into
Open
fix(zod-openapi): return 415 for request bodies with undeclared content type#2087WolfieLeader wants to merge 1 commit into
WolfieLeader wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: c6a9541 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Fixes #891.
Heads up: this could also have been fixed by defaulting
request.body.requiredtotrue. I went the other way because that turns genuinely optional bodies into 400s and changes the emitted OpenAPI document. Close this if you'd rather have the simpler default.The bug
For an optional body, the generated middleware validates only when the
Content-Typeheader matches a declared media type. Any other Content-Type skips validation and injects{}as the validated data. The client controls the header, so it can bypass validation on any route with an optional body.This route is enough to reproduce it:
Sending the same body under different Content-Types. The
Aftercolumn is this PR:Content-Type{"id":7}application/json{"id":7}{"id":7}{"id":7}application/vnd.api+json{"id":7}{"id":7}{"id":7}text/plain{}{"id":7}{}<post><id>7</id></post>application/xml{}<post><id>7</id></post>text/xml{}{}{}{"id":7}(required: true)text/plaindatais typed{ id: number }, sodata.idis anumberto the compiler while the runtime value isundefined.That last row is the only behavior change beyond the fix itself:
required: truewith a mismatched Content-Type moves from 400 to 415. Hono'svalidator()already substitutes{}on a mismatch, so the schema was failing on an empty object rather than on the real problem.The fix
A gate runs before the body validators:
{}passes through for optional bodies, andrequired: truestill fails the schema with 400.The issue thread proposed 400 for this case. This PR returns 415 because RFC 9110 defines it for exactly this situation. Switching to 400 is a one-line change if you prefer.
One gap: only JSON and form media types get a validator, so a route declaring
application/jsonandapplication/xmlstill gets{}when sent XML. Rejecting that would break XML-only routes, which are legitimate, so it needs a call.pnpm changesetat the top of this repo and push the changeset