{{ message }}
fix(utils): handle null in isJSONSerializable#599
Open
MFA-G wants to merge 1 commit into
Open
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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.