Skip to content

fix: check null before typeof in isJSONSerializable#578

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

fix: check null before typeof in isJSONSerializable#578
ruguoba wants to merge 1 commit into
unjs:mainfrom
ruguoba:fix/is-json-serializable-null

Conversation

@ruguoba
Copy link
Copy Markdown

@ruguoba ruguoba commented May 15, 2026

Problem

isJSONSerializable(null) throws:

Uncaught TypeError: Cannot read properties of null (reading buffer)
    at isJSONSerializable

Root Cause

typeof null returns "object", not null. So t === null (where t = typeof value) is always false — it is dead code. When value is null, it falls through to the object branch and crashes on value.buffer access.

Fix

Move the null check to use value === null before the typeof comparison:

- if (t === "string" || t === "number" || t === "boolean" || t === null) {
+ if (value === null || t === "string" || t === "number" || t === "boolean") {

Fixes #571

Summary by CodeRabbit

Bug Fixes

  • Fixed JSON serialization validation to correctly recognize null values as JSON-serializable, improving data type checking accuracy across the application.

Review Change Stack

typeof null returns 'object', not null, so t === null is always false
(dead code). When value is null, it falls through to the object branch
and crashes on value.buffer access.

Move null check to use value === null before typeof comparison.

Fixes #571
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8dd34ddc-c96c-486f-aa41-39f9d9b8642e

📥 Commits

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

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

📝 Walkthrough

Walkthrough

The isJSONSerializable utility function is corrected to properly identify null as JSON-serializable. The type-guard logic replaces a dead typeof check against null with an explicit value check, fixing a bug that caused the function to throw when passed null.

Changes

Null value JSON serialization

Layer / File(s) Summary
Null value JSON serialization check
src/utils.ts
The early-return condition in isJSONSerializable replaces t === null (unreachable since typeof never yields null) with value === null, correctly identifying null as JSON-serializable and preventing downstream null-pointer errors.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

A null that was lurking in shadows so deep,
Now sleeps as a JSON-safe value to keep,
One line, one check, the bug takes its flight—
The rabbit hops forward, the fix is just right! 🐰✨

🚥 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 change: moving the null check before the typeof operation in isJSONSerializable.
Linked Issues check ✅ Passed The PR successfully addresses issue #571 by implementing the exact fix needed: moving the null check to use value === null before typeof to properly identify null as JSON-serializable.
Out of Scope Changes check ✅ Passed The changes are narrowly scoped to the isJSONSerializable function with only a single line change (+1/-1), directly addressing the reported bug without introducing unrelated modifications.

✏️ 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