jsonpatch-js
jsonpatch-js copied to clipboard
Removes are completed sequentially, leading to unexpected results.
Example:
{"mydata": ["apple", "orange","pear", "lemon"]}
With the following operation:
[{"op":"remove","path":"/mydata/0"},{"op":"remove","path":"/mydata/2"}]
The result is:
{ "mydata": ["orange","pear"] }
Rather than the expected:
{ "mydata": ["orange", "lemon"] }
See here: https://github.com/fge/json-patch/issues/11#issue-31699289
Related thread: https://github.com/fge/json-patch/issues/11
Dug into this a little more and it appears as though the issue is when an array of patches are performed for an array.
Here is a failing test:
var obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]};
jsonpatch.apply(obj, [{op: 'remove', path: '/bar/0'},{op: 'remove', path: '/bar/1'}]);
deepEqual(obj, {foo: 1, baz: [{qux: 'hello'}], bar: [3, 4]});
Here's the test results:

@bruth - Any hope you may fix this?
Sorry about that, this issue completely slipped through the cracks. I sort of which GitHub would continue to bug you if a maintainer has not responded..
I will fix this.
The result is:
{ "mydata": ["orange","pear"] }Rather than the expected:
{ "mydata": ["orange", "lemon"] }
I don't understand the issue, patches are supposed to be applied sequentially.
https://tools.ietf.org/html/rfc6902
Evaluation of a JSON Patch document begins against a target JSON document. Operations are applied sequentially in the order they appear in the array. Each operation in the sequence is applied to the target document; the resulting document becomes the target of the next operation. Evaluation continues until all operations are successfully applied or until an error condition is encountered.
https://tools.ietf.org/html/rfc6902#section-4.2
If removing an element from an array, any elements above the specified index are shifted one position to the left.
Result looks fine.
["apple", "orange", "pear", "lemon"]
// remove /0 =>
["orange", "pear", "lemon"]
// remove /2 =>
["orange", "pear"]
and the issue you linked doesn't appear to have anything to do with your test case.