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

Incorrect diff generated for some object nodes #136

Open
Quantum64 opened this issue Aug 14, 2021 · 0 comments
Open

Incorrect diff generated for some object nodes #136

Quantum64 opened this issue Aug 14, 2021 · 0 comments

Comments

@Quantum64
Copy link

Quantum64 commented Aug 14, 2021

I only have a preliminary understanding of this issue. Reporting now because it is causing havoc in our recently launched production system. I am planning to thoroughly investigate and potentially PR a fix in the coming days.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flipkart.zjsonpatch.JsonDiff;

import java.util.HashMap;
import java.util.Map;

public class PatchTest {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) throws Exception {
        Map<Integer, String> first = new HashMap<>();
        first.put(1, "foo");
        first.put(33, "bar");

        Map<Integer, String> second = new HashMap<>();
        second.put(2, "foo");
        second.put(3, "bar");

        JsonNode source = mapper.convertValue(first, JsonNode.class);
        JsonNode target = mapper.convertValue(second, JsonNode.class);
        JsonNode diff = JsonDiff.asJson(source, target);

        System.out.println(mapper.writeValueAsString(source));
        System.out.println(mapper.writeValueAsString(target));
        System.out.println(mapper.writeValueAsString(diff));
    }
}

Incorrectly outputs

{"1":"foo","33":"bar"}
{"2":"foo","3":"bar"}
[{"op":"move","from":"/1","path":"/3"},{"op":"move","from":"/33","path":"/3"}]

Notice two values being moved to key 3.

Possible temporary workaround: Disable move operations.

Edit: Seems to be related specifically to integer map keys.
2nd edit: Seems to be related to integer map keys being misinterpreted as array indices.

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

1 participant