fix(utils): handle `null` in isJSONSerializable
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 === nullearly return.nullis JSON serializable (JSON.stringify(null) === "null"), so it returnstrue. - Remove the dead
t === nullcondition from the primitive check. - Add
test/utils.test.tswith unit tests forisJSONSerializablecoveringnull,undefined, primitives, plain objects,toJSONobjects, arrays, and non-serializable values (functions, symbols, bigint, typed arrays, FormData, URLSearchParams).
Validation
pnpm lintpasses (eslint + prettier).vitest runpasses (34 tests, including the 6 new ones).- Confirmed the new
nulltest fails against the current code (reproduces theTypeError) and passes with this fix.
Closes #571
Summary by CodeRabbit
-
Bug Fixes
- Corrected JSON serializability checks so
nullis now treated as serializable. - Improved handling of values that should not be considered JSON-safe, reducing incorrect results.
- Corrected JSON serializability checks so
-
Tests
- Added coverage for serializable and non-serializable values.
- Verified support for
null, objects, arrays, andtoJSON-based values, along with rejection of unsupported types.
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.tstest/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.
Comment @coderabbitai help to get the list of available commands.