ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

fix(utils): handle `null` in isJSONSerializable

Open MFA-G opened this issue 5 months ago β€’ 1 comments

Summary

isJSONSerializable(null) currently throws:

TypeError: Cannot read properties of null (reading 'buffer')

null falls through every early branch and reaches the value.buffer check (src/utils.ts line 31), where reading a property off null throws.

There is also a dead-code branch: typeof value can never return null, so the t === null part of the primitive check on line 22 never matches.

Changes

  • Add an explicit value === null early return. null is JSON serializable (JSON.stringify(null) === "null"), so it returns true.
  • Remove the dead t === null condition from the primitive check.
  • Add test/utils.test.ts with unit tests for isJSONSerializable covering null, undefined, primitives, plain objects, toJSON objects, arrays, and non-serializable values (functions, symbols, bigint, typed arrays, FormData, URLSearchParams).

Validation

  • pnpm lint passes (eslint + prettier).
  • vitest run passes (34 tests, including the 6 new ones).
  • Confirmed the new null test fails against the current code (reproduces the TypeError) and passes with this fix.

Closes #571

Summary by CodeRabbit

  • Bug Fixes

    • Corrected JSON serializability checks so null is now treated as serializable.
    • Improved handling of values that should not be considered JSON-safe, reducing incorrect results.
  • Tests

    • Added coverage for serializable and non-serializable values.
    • Verified support for null, objects, arrays, and toJSON-based values, along with rejection of unsupported types.

MFA-G avatar Jun 25 '26 07:06 MFA-G

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: 29e83460-96ad-4c6e-8b31-061b6c6dca33

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between dfbe3ca4ef8a22fc023fca5a5ef530e525f5e523 and 12274d10a0535fbd545040bcdade7885ceb72e5f.

πŸ“’ Files selected for processing (2)
  • src/utils.ts
  • test/utils.test.ts

πŸ“ Walkthrough

Walkthrough

isJSONSerializable now treats null as serializable before type checks. A new test suite covers null, primitives, objects, arrays, toJSON-based values, and several non-serializable inputs.

Changes

Null serialization behavior

Layer / File(s) Summary
Null handling and serializability tests
src/utils.ts, test/utils.test.ts
isJSONSerializable returns true for null before inspecting typeof, and the Vitest suite checks null, primitives, plain objects/arrays, toJSON support, and rejected non-JSON values.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped to the null and tapped the gate,
β€œYou’re JSON-safe,” said the carrot crate.
Primitives twinkled, arrays stayed bright,
Odd values stayed out, all snug and right.
Hooray for tidy burrows tonight!

πŸš₯ 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 matches the main change: fixing null handling in isJSONSerializable.
Linked Issues check βœ… Passed The code and tests address #571 by making null serializable and preventing the null buffer TypeError.
Out of Scope Changes check βœ… Passed The changes are scoped to the null-serialization fix and its test coverage, with no unrelated edits.

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

coderabbitai[bot] avatar Jun 25 '26 07:06 coderabbitai[bot]