Skip to content

fix: throw TypeError for non-JSON-serializable values in c.json()#4756

Open
veeceey wants to merge 2 commits intohonojs:mainfrom
veeceey:fix/issue-2343-json-undefined-throw
Open

fix: throw TypeError for non-JSON-serializable values in c.json()#4756
veeceey wants to merge 2 commits intohonojs:mainfrom
veeceey:fix/issue-2343-json-undefined-throw

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 23, 2026

Fixes #2343

When c.json(undefined) is called, JSON.stringify(undefined) returns undefined (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.stringify and throw a TypeError if 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 of Response.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.

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
@veeceey
Copy link
Author

veeceey commented Feb 23, 2026

Test Results

Ran the full test suite - all context tests pass including the new test:

✓ |main| src/context.test.ts (58 tests) 17ms
  Tests  58 passed (58)

Full suite:

Test Files  1 failed | 143 passed (144)
     Tests  4 failed | 4089 passed | 33 skipped (4126)

The 4 pre-existing failures are in runtime-tests/node/index.test.ts (stream abort handler tests) and unrelated to this change.

Manual verification:

// Before: JSON.stringify(undefined) → undefined → new Response(undefined) → "" (Bun) / "undefined" (CF)
// After: throws TypeError("Value is not JSON serializable")

@codecov
Copy link

codecov bot commented Feb 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.48%. Comparing base (2de30d7) to head (d5fb0e9).
⚠️ Report is 4 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe yusukebe mentioned this pull request Feb 23, 2026
4 tasks
@yusukebe
Copy link
Member

@veeceey

Thank you for the PR. If this issue is introduced by #4707, #4757 fixed the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

c.json returning empty string for undefined values

2 participants