Skip to content

Incorrect diff generated for some object nodes #136

@Quantum64

Description

@Quantum64

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions