fix: isJSONSerializable returns true for null (#571) by rafaumeu · Pull Request #596 · unjs/ofetch · GitHub
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

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