Skip to content

fix: treat null as JSON-serializable (isJSONSerializable(null) throws) - #625

Open
DanMat wants to merge 1 commit into
unjs:mainfrom
DanMat:fix/is-json-serializable-null
Open

fix: treat null as JSON-serializable (isJSONSerializable(null) throws)#625
DanMat wants to merge 1 commit into
unjs:mainfrom
DanMat:fix/is-json-serializable-null

Conversation

@DanMat

@DanMat DanMat commented Aug 18, 2026

Copy link
Copy Markdown

Closes #571.

isJSONSerializable(null) currently throws Cannot read properties of null (reading 'buffer'):

  • t === null is dead code — typeof always returns a string, never null.
  • So null isn't caught there, falls past the t !== "object" guard, and reaches value.buffer, which throws.

null is valid JSON (JSON.stringify(null) === "null"), so it should return true. This replaces the dead t === null check with value === null, catching null before the .buffer access.

Also adds test/utils.test.ts — the util had no direct unit test — covering null, primitives, plain objects/arrays, and FormData/URLSearchParams. All existing tests still pass; lint clean.


Disclosure: this change was written with AI assistance. I've reviewed, tested, and understand it, and I'll maintain it.

Summary by CodeRabbit

  • Bug Fixes

    • Corrected JSON serialization checks so null is recognized as a supported value without causing errors.
  • Tests

    • Added coverage for primitives, objects, arrays, custom serialization, form-related types, URL search parameters, and unsupported values.

isJSONSerializable(null) threw "Cannot read properties of null (reading
'buffer')": `t === null` was dead code (typeof returns a string, never null),
so null fell through to `value.buffer`. null is valid JSON, so it should return
true. Replace the dead check with `value === null`. Adds a unit test for the
util (previously untested).

Closes unjs#571
@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 5534bc66-782e-4fc2-9a4f-abf86fa76372

📥 Commits

Reviewing files that changed from the base of the PR and between 1dbc37f and 363d92f.

📒 Files selected for processing (2)
  • src/utils.ts
  • test/utils.test.ts

Included review availability: Your plan includes up to 8 reviews per rolling hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

isJSONSerializable now correctly handles null without throwing. Vitest coverage validates null, supported JSON values, custom toJSON, and unsupported values.

Changes

JSON serialization validation

Layer / File(s) Summary
Null handling and serialization coverage
src/utils.ts, test/utils.test.ts
isJSONSerializable now accepts null. Tests cover JSON-serializable values and reject undefined, bigint, functions, FormData, and URLSearchParams.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: ⚪ Minimal · up to 363d9

This change makes null correctly report as JSON-serializable and adds focused coverage for the utility; no actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states that the change fixes null handling in isJSONSerializable.
Linked Issues check ✅ Passed The code fixes the null check that caused isJSONSerializable(null) to throw and adds direct test coverage for the reported bug [#571].
Out of Scope Changes check ✅ Passed The implementation and tests are limited to the null-handling bug and its required coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

isJSONSerializable bug

1 participant