Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.79k stars 215 forks source link

Unescape paths (during moves) #268

Closed bruce-one closed 3 years ago

bruce-one commented 3 years ago

Hello :-)

Thank you for your work on this library, it's awesome :-)

I was trying to do a move operation using a path that had an escape in it an it failed with:

OPERATION_FROM_UNRESOLVABLE: Cannot perform the operation from a path that does not exist

I've added a test based on that move which would then fail with:

    OPERATION_FROM_UNRESOLVABLE: Cannot perform the operation from a path that does not exist
    name: OPERATION_FROM_UNRESOLVABLE
    index: 0
    operation: {
      "op": "move",
      "from": "/foo~1/bar~1",
      "path": "/bar"
    }
    tree: {
      "foo/": {
        "bar/": 1,
        "baz": 1
      }
    }

This PR moves the unescape operation earlier (before then using the key in the other branches) which fixes this scenario (afaiu) :-)

Sefriol commented 3 years ago

Noticed the same thing today and managed to come to same solution as you did before I found your PR.

This fix is definitely required.

Sefriol commented 3 years ago

As an extra note, this does not only affect moves. None of the patches will be able to pass patch validation check here unless the operation is add:

https://github.com/Starcounter-Jack/JSON-Patch/blob/fc6c69135d03e0cc2def40186dc4c36894974d18/commonjs/core.js#L340

@Starcounter-Jack

Starcounter-Jack commented 3 years ago

Sorry it took so long.