Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/main/java/com/github/fge/jsonpatch/diff/DiffProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ private JsonPointer findUnchangedValue(final JsonNode value)

private int findPreviouslyRemoved(final JsonNode value)
{
DiffOperation op;

for (int i = 0; i < diffs.size(); i++) {
op = diffs.get(i);
if (op.getType() == DiffOperation.Type.REMOVE
&& EQUIVALENCE.equivalent(value, op.getOldValue()))
return i;
}
if (diffs.isEmpty())
return -1;

// Intervening operations may depend on a removal having already
// happened, especially when it shifts subsequent array elements.
final int index = diffs.size() - 1;
final DiffOperation op = diffs.get(index);
if (op.getType() == DiffOperation.Type.REMOVE
&& EQUIVALENCE.equivalent(value, op.getOldValue()))
return index;
return -1;
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/github/fge/jsonpatch/diff/JsonDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package com.github.fge.jsonpatch.diff;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jackson.JsonNumEquals;
import com.github.fge.jsonpatch.JsonPatch;
Expand Down Expand Up @@ -58,6 +60,30 @@ public Iterator<Object[]> getPatchesOnly()
return list.iterator();
}

@Test
public void smallArrayPairsRoundTrip()
throws IOException, JsonPatchException
{
final JsonNode arrays = JsonLoader.fromString(
"[[], [0], [1], [0, 0], [0, 1], [1, 0], [1, 1]]");
final List<JsonNode> documents = Lists.newArrayList();
for (final JsonNode left: arrays)
for (final JsonNode right: arrays) {
final ObjectNode document = JsonNodeFactory.instance.objectNode();
document.set("a", left);
document.set("b", right);
documents.add(document);
}

for (final JsonNode source: documents)
for (final JsonNode target: documents) {
final JsonNode actual = JsonDiff.asJsonPatch(source, target)
.apply(source);
assertThat(actual).as("patch from %s to %s", source, target)
.isEqualTo(target);
}
}

@Test(dataProvider = "getPatchesOnly")
public void generatedPatchAppliesCleanly(final JsonNode first,
final JsonNode second)
Expand Down
22 changes: 21 additions & 1 deletion src/test/resources/jsonpatch/diff/diff.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,25 @@
"patch": [
{ "op": "move", "path": "/c", "from": "/a" }
]
},
{
"message": "a moved value must not be postponed past another array removal",
"first": { "a": [1, 2], "b": [] },
"second": { "a": [], "b": [1] }
},
{
"message": "a removed array prefix must not change the moved value",
"first": { "a": [0, 1, 2], "b": [3] },
"second": { "a": [0], "b": [3, 1] }
},
{
"message": "nested arrays must preserve moved objects",
"first": { "a": [{"x": 1}, {"x": 2}], "b": {"c": []} },
"second": { "a": [], "b": {"c": [{"x": 1}]} }
},
{
"message": "the last removal can still be factored into a move",
"first": { "a": [1, 2], "b": [] },
"second": { "a": [], "b": [2] }
}
]
]