immer icon indicating copy to clipboard operation
immer copied to clipboard

'Cannot apply patch, path doesn't resolve' when modifying property of an object in Map

Open AhsanSarwar45 opened this issue 3 years ago • 1 comments

🐛 Bug Report

applyPatches throws error Cannot apply patch, path doesn't resolve: map/1/b when applying inversePatches generated by the following produce call:

interface Test {
  a: number;
  b: string;
}

let state = {
  map: new Map<number, Test>([])
};

state.map.set(1, { a: 5, b: "hello" }); // Setting initial state

let inversePatches: Patch[] = [];

state = produce(
    state,
    (draftState) => {
      const test = draftState.map.get(1);
      if (test) test.b = "world";
    },
    (_patches, _inversePatches) => {
      inversePatches = _inversePatches;
    }
);

It works fine when the patch op is add instead of replace:

state = produce(
    state,
    (draftState) => {
      const test = draftState.map.get(1);
      if (test) draftState.map.set(1, {...test, b: "world"}); // works
    },
    (_patches, _inversePatches) => {
      inversePatches = _inversePatches;
    }
);

Link to repro

https://codesandbox.io/s/awesome-stitch-xxtxq3?file=/src/index.ts

  • Click Set Map Property button to set map property
  • Click Undo button to call applyPatches with inversePatches

To Reproduce

Steps to reproduce the behavior:

  • Call enableMapSet and enablePatches
  • Create a Map with an object as the value type and add some key-value pairs to it
  • Use produce to mutate the Map
  • Inside produce, retrieve an element using Map.get() and modify a property on the element
  • Save the inversePatches generated by produce
  • Call applyPatches(map, inversePatches)

Observed behavior

applyPatches throws error Cannot apply patch, path doesn't resolve: map/1/b.

Expected behavior

The inversePatches should be applied successfully and state should revert back to the state it was in before the produce call

Environment

  • Immer version: 9.0.14
  • [x] I filed this report against the latest version of Immer
  • [x] Occurs with setUseProxies(true)
  • [x] Occurs with setUseProxies(false) (ES5 only)

AhsanSarwar45 avatar Jun 13 '22 19:06 AhsanSarwar45

Getting the same error. Any updates on this?

azeem-io avatar Jul 20 '22 18:07 azeem-io

:tada: This issue has been resolved in version 9.0.20 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Mar 23 '23 17:03 github-actions[bot]