fix: throw TypeError for non-JSON-serializable values in c.json()#4756
Open
veeceey wants to merge 2 commits intohonojs:mainfrom
Open
fix: throw TypeError for non-JSON-serializable values in c.json()#4756veeceey wants to merge 2 commits intohonojs:mainfrom
veeceey wants to merge 2 commits intohonojs:mainfrom
Conversation
When passing undefined or other non-serializable values to c.json(), JSON.stringify returns undefined which leads to inconsistent behavior across runtimes - some return empty string, others "undefined" text. This follows the same approach as Deno's implementation: check the result of JSON.stringify and throw a TypeError if the value is not serializable. This gives a clear, consistent error across all runtimes instead of silently returning wrong responses. Fixes honojs#2343
Author
Test ResultsRan the full test suite - all context tests pass including the new test: Full suite: The 4 pre-existing failures are in Manual verification: // Before: JSON.stringify(undefined) → undefined → new Response(undefined) → "" (Bun) / "undefined" (CF)
// After: throws TypeError("Value is not JSON serializable") |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4756 +/- ##
=======================================
Coverage 91.48% 91.48%
=======================================
Files 177 177
Lines 11556 11556
Branches 3357 3359 +2
=======================================
Hits 10572 10572
Misses 983 983
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
4 tasks
Member
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 #2343
When
c.json(undefined)is called,JSON.stringify(undefined)returnsundefined(not a string), which leads to inconsistent behavior across runtimes - Bun returns an empty string"", Cloudflare returns"undefined", and Node throws an error.This follows the same approach discussed in the issue thread, matching Deno's implementation: check the result of
JSON.stringifyand throw aTypeErrorif the value isn't serializable. This way the behavior is consistent across all runtimes instead of silently returning wrong responses.Also moved the fast path to use
new Response(body, ...)instead ofResponse.json(object)so the serialization check happens before sending the response regardless of which code path is taken.Added a test to verify the behavior.