fix(utils): handle null correctly in isJSONSerializable#595
fix(utils): handle null correctly in isJSONSerializable#595chatman-media wants to merge 1 commit into
Conversation
- Remove dead code: `typeof value` never returns `"null"`, so `t === null` was always false and the branch never matched. - Add an early `value === null` guard that returns `true`; null is JSON-serializable (`JSON.stringify(null) === "null"`). - The guard must come before any property access on `value` — without it, `isJSONSerializable(null)` throws `TypeError: Cannot read properties of null (reading 'buffer')`. - Add a dedicated `isJSONSerializable` describe block in the test suite covering null (no-throw + correct return value), undefined, primitives, non-serializable types, plain objects/arrays, toJSON, Buffer/TypedArray, FormData, and URLSearchParams. Closes unjs#571
|
Linter diff in the way? Review this PR in Change Stack to focus on meaningful changes and expand context only when needed. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PR fixes a bug in Changesnull handling and test coverage
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Closes #571
What was wrong
isJSONSerializableinsrc/utils.tshad three bugs when called withnull:t === nullon line 22 (wheret = typeof value) can never be true.typeofnever returns the string"null", so this branch was unreachable.nullis valid JSON (JSON.stringify(null) === "null"), but the function never identified it as serializable.nullwasn't caught early, execution reachedvalue.buffer, which throwsTypeError: Cannot read properties of null (reading 'buffer').Fix
Add an explicit
value === nullguard that returnstruebefore any property access onvalue, and drop the deadt === nullbranch from thetypeofcheck:undefinedand all other existing code paths are unaffected.Tests
Added a dedicated
describe("isJSONSerializable")block intest/index.test.tscovering:null— does not throw, returnstrueundefined— returnsfalsestring,number,boolean) — returntruefunction,symbol,bigint) — returnfalsetruetoJSON— returntrueBuffer/Uint8Array— returnfalseFormData/URLSearchParams— returnfalseThe
nulltest fails on the original code (TypeError: Cannot read properties of null (reading 'buffer')) and passes with this fix.🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests