flipkart-incubator / flipkart-incubator/zjsonpatch
Incorrect diff generated for some object nodes
- Dominant language
- Java
- Stars
- 586
- Forks
- 153
- PR merge metrics
- No merged PRs in 30d
Description
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.
```java
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 first = new HashMap<>();
first.put(1, "foo");
first.put(33, "bar");
Map 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.
Contributor guide
Research direction
Reproduce the report using the Java PatchTest example and JsonDiff.asJson with integer-keyed maps. Then trace diff generation and JSON Pointer handling for those object keys; done means the generated patch no longer sends both values to /3 and correctly transforms the source into the target.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100