fix(utils): handle null in isJSONSerializable#599
Conversation
`isJSONSerializable(null)` threw `TypeError: Cannot read properties of null (reading 'buffer')` because `null` fell through to the `value.buffer` check. Also `typeof value` never returns `null`, so the `t === null` branch was dead code. `null` is JSON serializable (`JSON.stringify(null) === "null"`), so it now returns `true` via an explicit early check. Added unit tests for isJSONSerializable covering null, undefined, primitives, plain objects, toJSON objects, and non-serializable values. Closes unjs#571
|
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)
📝 WalkthroughWalkthrough
ChangesNull serialization behavior
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 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 |
Summary
isJSONSerializable(null)currently throws:nullfalls through every early branch and reaches thevalue.buffercheck (src/utils.tsline 31), where reading a property offnullthrows.There is also a dead-code branch:
typeof valuecan never returnnull, so thet === nullpart of the primitive check on line 22 never matches.Changes
value === nullearly return.nullis JSON serializable (JSON.stringify(null) === "null"), so it returnstrue.t === nullcondition from the primitive check.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).nulltest fails against the current code (reproduces theTypeError) and passes with this fix.Closes #571
Summary by CodeRabbit
Bug Fixes
nullis now treated as serializable.Tests
null, objects, arrays, andtoJSON-based values, along with rejection of unsupported types.