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

Diff then patch fails on certain json #116

Open
robseed opened this issue Feb 25, 2020 · 2 comments
Open

Diff then patch fails on certain json #116

robseed opened this issue Feb 25, 2020 · 2 comments

Comments

@robseed
Copy link

robseed commented Feb 25, 2020

Expected Behavior

If JsonDiff.asJson succeeds then JsonPatch.apply should succeed

Actual Behavior

JsonPatch.apply fails with [MOVE Operation] Missing field

Steps to Reproduce the Problem

ObjectMapper mapper = new ObjectMapper();
JsonNode source = mapper.readTree("{\"16\":[{\"id\":\"one\",\"type\":\"page\",\"length\":1}],\"63\":[{\"id\":\"two\",\"length\":2}],\"76\":[{\"id\":\"three\",\"length\":3}]}");
JsonNode target = mapper.readTree("{\"16\":[{\"id\":\"four\",\"length\":4}],\"76\":[{\"id\":\"five\",\"type\":\"page\",\"length\":5}]}");
JsonNode diff = JsonDiff.asJson(source, target);
JsonPatch.apply(diff, source);

[MOVE Operation] Missing field "77" at root
	at com.flipkart.zjsonpatch.JsonPatch.process(JsonPatch.java:112)
	at com.flipkart.zjsonpatch.JsonPatch.apply(JsonPatch.java:127)
	at com.flipkart.zjsonpatch.JsonPatch.apply(JsonPatch.java:132)

Specifications

The json looks contrived, but it is a real world example from comparing facebook posts.
Java 1.8, Jackson 2.9.9, zjsonpatch 0.4.9

@holograph
Copy link
Collaborator

Error reproduced, I'm looking into it. Especially with the error message it is indeed quite strange.

@holograph
Copy link
Collaborator

OK, the problem has to do with JSON Pointer semantics. When processing a list of diffs to introduce move ops, the assumption is the a numerical path segment is an array index (which means it has to be taken into account in adjusting the resulting paths).

That is incorrect: JSON Pointer doesn't actually know whether a path segment refers to array or object. For example, the path /0/abc will correctly evaluate against both of these documents: {"0": {"abc": true}} and [{"abc":true}].

Solving this requires modifying JsonDiff.introduceMoveOperation and JsonDiff.updatePath to take the source document into account; I'm working on it, but it'll take a bit of time.

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