Skip to content

fix: isJSONSerializable returns true for null (#571)#596

Open
rafaumeu wants to merge 1 commit into
unjs:mainfrom
rafaumeu:fix/isjsonserializable-null-571
Open

fix: isJSONSerializable returns true for null (#571)#596
rafaumeu wants to merge 1 commit into
unjs:mainfrom
rafaumeu:fix/isjsonserializable-null-571

Conversation

@rafaumeu

@rafaumeu rafaumeu commented Jun 17, 2026

Copy link
Copy Markdown

Fixes #571

Bug Description:

The function had a critical bug on line 22 where it checked t === null,
but typeof never returns "null" (typeof null === "object").

This caused three issues:

  1. Dead code: t === null was always false
  2. isJSONSerializable(null) threw TypeError: "Cannot read properties of null (reading buffer)"
  3. null was incorrectly identified as not JSON serializable

Changes:

  • Added explicit value === null check before typeof comparison
  • Removed t === null from typeof checks (impossible condition)

Testing:

  • Added comprehensive unit tests (test/isjsonserializable.test.ts)
  • Tests cover: null, undefined, strings, numbers, booleans, arrays, objects, FormData, URLSearchParams, buffers
  • All 10 tests pass

Summary by CodeRabbit

  • Bug Fixes

    • Fixed a bug where null values were incorrectly handled during JSON serialization checks, causing improper processing in downstream operations.
  • Tests

    • Added comprehensive test suite validating proper handling of null, undefined, primitives, arrays, objects, and various non-serializable types.

Fixes unjs#571

The bug was on line 22 where it checked `t === null`,
but typeof never returns "null" (typeof null === "object").

This caused:
1. Dead code: t === null was always false
2. isJSONSerializable(null) threw TypeError accessing null.buffer
3. null was incorrectly identified as not JSON serializable

Fixed by:
- Adding explicit null check before typeof comparison
- Removing t === null from typeof checks (impossible condition)

Tests:
- Added comprehensive unit tests for isJSONSerializable
- All tests pass including null, undefined, primitives, and edge cases
@coderabbitai

coderabbitai Bot commented Jun 17, 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

Run ID: 59a05958-495b-4ad3-bbe3-4fb39f77f6f2

📥 Commits

Reviewing files that changed from the base of the PR and between dfbe3ca and f593d42.

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

📝 Walkthrough

Walkthrough

Fixes a bug in isJSONSerializable where passing null would fall through to object-handling logic (since typeof null === "object") and throw a TypeError. An explicit value === null guard is added to return true early. A new Vitest test file covers null, primitives, arrays, plain objects, and non-serializable browser types.

Changes

isJSONSerializable null fix and tests

Layer / File(s) Summary
null guard fix and test coverage
src/utils.ts, test/isjsonserializable.test.ts
isJSONSerializable adds an explicit value === null early-return of true, replacing the dead typeof value === null check. New test suite asserts true for null, primitives, arrays, and plain objects, and false for ArrayBuffer, FormData, and URLSearchParams.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐇 A null walked in, was turned away,
"Not serializable!" the function would say.
But null IS valid JSON, friend — it's true!
A simple === null check fixes the brew.
Now tests confirm what the spec always knew. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: handling null values correctly in isJSONSerializable function.
Linked Issues check ✅ Passed Code changes fully address all objectives from issue #571: null is now explicitly handled, dead code removed, TypeError prevented, and null correctly identified as JSON-serializable.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the isJSONSerializable null-handling bug; no unrelated modifications were introduced.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

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