Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes are completed sequentially, leading to unexpected results. #23

Open
jonkeegan opened this issue Jan 7, 2016 · 3 comments
Open
Labels

Comments

@jonkeegan
Copy link

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:
java-json-tools/json-patch#11 (comment)

Related thread: java-json-tools/json-patch#11

@sturdynut
Copy link

sturdynut commented Apr 18, 2016

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?

@bruth
Copy link
Owner

bruth commented Apr 19, 2016

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 bruth added the bug label Apr 19, 2016
@sonnyp
Copy link

sonnyp commented May 17, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants