Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1596,3 +1596,12 @@ describe("RTK-5159: Patch path truncation bug", () => {
expect(patches[0].path).toEqual(["queries", "queryKey", "data", "items", 1])
})
})
test("applyPatches throws proper immer error when intermediate path value is null", () => {
// isObjectish(null) === true (typeof null === "object"), so the null check
// inside applyPatches_ was missing; navigating into null threw a native
// TypeError instead of immer's own "path doesn't resolve" error.
const isProd = process.env.NODE_ENV === "production"
expect(() => {
applyPatches({a: null}, [{op: "add", path: ["a", "b"], value: 1}])
}).toThrow(isProd ? "18" : "Cannot apply patch, path doesn't resolve: a/b")
})
3 changes: 2 additions & 1 deletion src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ export function enablePatches() {
die(errorOffset + 3)
if (isFunction(base) && p === PROTOTYPE) die(errorOffset + 3)
base = get(base, p)
if (!isObjectish(base)) die(errorOffset + 2, path.join("/"))
if (base === null || !isObjectish(base))
die(errorOffset + 2, path.join("/"))
}

const type = getArchtype(base)
Expand Down