fix: throw proper immer error when applyPatches path traverses null#1251
Open
JSap0914 wants to merge 1 commit into
Open
fix: throw proper immer error when applyPatches path traverses null#1251JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
typeof null === "object" causes isObjectish(null) to return true,
silently bypassing the path-resolution guard inside applyPatches_.
Traversal then calls getArchtype(null) which accesses null[DRAFT_STATE]
and crashes with a native TypeError instead of immer error 18
("Cannot apply patch, path doesn't resolve").
Add an explicit null check alongside isObjectish so the guard fires
correctly and the caller receives the expected immer error message.
Fixes: applyPatches({a: null}, [{op: 'add', path: ['a', 'b'], value: 1}])
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.
Bug
applyPatchesthrows a nativeTypeErrorinstead of immer's own error when a patch path navigates through anullintermediate value.Root cause:
isObjectishis implemented astypeof target === "object". Becausetypeof null === "object"in JavaScript,isObjectish(null)returnstrue. The guard insideapplyPatches_that is supposed to catch non-navigable intermediates therefore letsnullslip through. The next iteration (or the post-loopgetArchtypecall) then doesnull[DRAFT_STATE]and crashes with:instead of immer error 18:
Fix
Add an explicit
base === nullcheck alongside!isObjectish(base)in the path-traversal loop ofapplyPatches_(src/plugins/patches.ts):Verification
New test added in
__tests__/patch.js:applyPatches({a: null}, [{op: 'add', path: ['a', 'b'], value: 1}])now throws with message"Cannot apply patch, path doesn't resolve: a/b"