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.77k stars 213 forks source link

Validation fails for patches with JSON pointer escapes (OPERATION_PATH_UNRESOLVABLE) #253

Open bverhoeven opened 4 years ago

bverhoeven commented 4 years ago

We're experiencing when applying patches with JSON pointer escapes (~1') using fast-json-patch (3.0.0-1).

From what I read, JSON pointers need to have their slashes escaped:

Because the characters '~' (%x7E) and '/' (%x2F) have special
meanings in JSON Pointer, '~' needs to be encoded as '~0' and '/'
needs to be encoded as '~1' when these characters appear in a
reference token.

Test data:

Result:

OPERATION_PATH_UNRESOLVABLE: Cannot perform the operation at a path that does not exist
name: OPERATION_PATH_UNRESOLVABLE
index: 0
operation: {
  "op": "replace",
  "path": "/hello~1world",
  "value": 42
}
tree: {
  "hello/world": 0
}
    at new PatchError (node_modules/fast-json-patch/commonjs/helpers.js:170:28)
   ....
  name: 'OPERATION_PATH_UNRESOLVABLE',
  index: 0,
  operation: { op: 'replace', path: '/hello~1world', value: 42 },
  tree: { 'hello/world': 0 }
}

Code:

let source = JSON.parse(`{ "hello/world": 0 }`)
let patch  = JSON.parse(`[{"op":"replace","path":"/hello~1world","value":42}]`)

// This works:
console.log(fastJsonPatch.applyPatch(source, patch, false).newDocument)

// This fails with "OPERATION_PATH_UNRESOLVABLE":
fastJsonPatch.applyPatch(source, patch, true)
Starcounter-Jack commented 3 years ago

This is indeed an incorrect behaviour. A pull request would be most welcome. If not, I will put it in the backlog.

tedyu commented 3 months ago

We've hit this error. Is there a new release where this is fixed ?