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

Wrong Path when comparing Lists #134

Open
upsidedownsmile opened this issue May 24, 2021 · 2 comments
Open

Wrong Path when comparing Lists #134

upsidedownsmile opened this issue May 24, 2021 · 2 comments

Comments

@upsidedownsmile
Copy link

upsidedownsmile commented May 24, 2021

Expected Behavior

source: "a", "b", "c"

target: "c", "b", "a"

When comparing these 2 lists it is expected 4 changes:
1 - "c" was added, path is "/0"
2 - "b" was added, path is "/1"
3 - "b" was removed, path was "/1"
4 - "c" was removed, path was "/2"

Actual Behavior

Instead, I'm getting this:
1 - "c" was added, path is "/0"
2 - "b" was added, path is "/1"
3 - "b" was removed, path was "/3"
4 - "c" was removed, path was "/3"

Steps to Reproduce the Problem

final JsonNode base = mapper.valueToTree(ImmutableList.of("a", "b", "c"));
final JsonNode target = mapper.valueToTree(ImmutableList.of("c", "b", "a"));

JsonDiff.asJson(serializedBaseObject, serializedTargetObject, EnumSet.of(DiffFlags.OMIT_MOVE_OPERATION));

Library Version:
zjsonpatch version: 0.4.11
java version: 1.8

@upsidedownsmile upsidedownsmile changed the title Wrong Path when comparing arrays Wrong Path when comparing Lists May 24, 2021
@LouizFC
Copy link
Collaborator

LouizFC commented May 24, 2021

@upsidedownsmile The behaviour is correct.

JsonPatch is applied "step by step". If you debug it like so, you get:

source: ["a", "b", "c"] -> target: ["c", "b", "a"]

"c" was added, path is "/0" -> ["c", "a", "b", "c"]
"b" was added, path is "/1" -> ["c", "b", "a", "b", "c"]
"b" was removed, path was "/3" -> ["c", "b", "a", "c"]
"c" was removed, path was "/3" -> ["c", "b", "a"]
result: ["c", "b", "a"]

@upsidedownsmile
Copy link
Author

upsidedownsmile commented May 24, 2021

Thanks for the explanation, I didn't know that it worked like that.

But, according to the README:

here "op" specifies the operation ("move"), "from" specifies the path from where the value should be moved, and "path" specifies where value should be moved.

If we include MOVE changes by removing the flag DiffFlags.OMIT_MOVE_OPERATION, we will get 2 changes (which is my use-case):
1 - {"op":"move","from":"/2","path":"/0"}
2 - {"op":"move","from":"/2","path":"/1"}

The path is not consistent with what is in the README.

I understand that is works the same way as you explained, but is there a way to get the value that was moved? (the only way that I found was to call source.at(<from path>) which in this case it will give me the wrong value for the second change.)

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

No branches or pull requests

2 participants