From 17f37d55663ffe85e8f7b3ab80657251d407f123 Mon Sep 17 00:00:00 2001 From: Jaehong Park <105151063+PHJ2000@users.noreply.github.com> Date: Thu, 22 Oct 2026 21:54:55 +0900 Subject: [PATCH] Preserve removal ordering when factoring JSON patch moves Signed-off-by: Jaehong Park <105151063+PHJ2000@users.noreply.github.com> --- .../fge/jsonpatch/diff/DiffProcessor.java | 18 +++++++------ .../fge/jsonpatch/diff/JsonDiffTest.java | 26 +++++++++++++++++++ src/test/resources/jsonpatch/diff/diff.json | 22 +++++++++++++++- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/github/fge/jsonpatch/diff/DiffProcessor.java b/src/main/java/com/github/fge/jsonpatch/diff/DiffProcessor.java index 299d29a1..cf16611a 100644 --- a/src/main/java/com/github/fge/jsonpatch/diff/DiffProcessor.java +++ b/src/main/java/com/github/fge/jsonpatch/diff/DiffProcessor.java @@ -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; } } diff --git a/src/test/java/com/github/fge/jsonpatch/diff/JsonDiffTest.java b/src/test/java/com/github/fge/jsonpatch/diff/JsonDiffTest.java index 1a8d0ac5..5f8fec6f 100644 --- a/src/test/java/com/github/fge/jsonpatch/diff/JsonDiffTest.java +++ b/src/test/java/com/github/fge/jsonpatch/diff/JsonDiffTest.java @@ -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; @@ -58,6 +60,30 @@ public Iterator 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 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) diff --git a/src/test/resources/jsonpatch/diff/diff.json b/src/test/resources/jsonpatch/diff/diff.json index 243aef12..b320755a 100644 --- a/src/test/resources/jsonpatch/diff/diff.json +++ b/src/test/resources/jsonpatch/diff/diff.json @@ -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] } } -] \ No newline at end of file +]