jsonpatch-js icon indicating copy to clipboard operation
jsonpatch-js copied to clipboard

Removes are completed sequentially, leading to unexpected results.

Open jonkeegan opened this issue 9 years ago • 3 comments
trafficstars

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

jonkeegan avatar Jan 07 '16 19:01 jonkeegan

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: screen shot 2016-04-18 at 4 59 58 pm

@bruth - Any hope you may fix this?

sturdynut avatar Apr 18 '16 23:04 sturdynut

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.

bruth avatar Apr 19 '16 00:04 bruth

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.

sonnyp avatar May 17 '16 15:05 sonnyp