From 870174adde8aef34d5c30ead94ea96f5b87855d4 Mon Sep 17 00:00:00 2001 From: Gopi Vishwakarma Date: Mon, 28 Jul 2014 03:06:22 +0530 Subject: [PATCH] bug fix in move operation --- .../com/github/fge/jsonpatch/diff/DiffFactorizer.java | 9 ++++++++- src/test/resources/jsonpatch/factorizing-diff.json | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/fge/jsonpatch/diff/DiffFactorizer.java b/src/main/java/com/github/fge/jsonpatch/diff/DiffFactorizer.java index 9ca8bfe8..dfb46417 100644 --- a/src/main/java/com/github/fge/jsonpatch/diff/DiffFactorizer.java +++ b/src/main/java/com/github/fge/jsonpatch/diff/DiffFactorizer.java @@ -25,6 +25,8 @@ import com.google.common.base.Equivalence; import com.google.common.collect.Lists; +import java.util.Collection; +import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -106,6 +108,7 @@ public static void factorizeDiffs(final List diffs) private static void findPairs(final List diffs) { final int diffsSize = diffs.size(); + Collection alreadyPaired = new HashSet(); Diff addition, removal; @@ -119,7 +122,7 @@ private static void findPairs(final List diffs) */ for (int removeIndex = 0; removeIndex < diffsSize; removeIndex++) { removal = diffs.get(removeIndex); - if (removal.operation != DiffOperation.REMOVE) + if (removal.operation != DiffOperation.REMOVE || alreadyPaired.contains(removal)) continue; if (!EQUIVALENCE.equivalent(removal.value, addition.value)) continue; @@ -131,6 +134,10 @@ private static void findPairs(final List diffs) addition.firstOfPair = addIndex < removeIndex; removal.pairedDiff = addition; removal.firstOfPair = removeIndex < addIndex; + + alreadyPaired.add(removal); + alreadyPaired.add(addition); + break; } } diff --git a/src/test/resources/jsonpatch/factorizing-diff.json b/src/test/resources/jsonpatch/factorizing-diff.json index 98cd3056..2f7ec39a 100644 --- a/src/test/resources/jsonpatch/factorizing-diff.json +++ b/src/test/resources/jsonpatch/factorizing-diff.json @@ -296,5 +296,13 @@ { "op": "remove", "path": "/0" }, { "op": "remove", "path": "/1" } ] + }, + { + "first": { "b": [0, 1, 2] }, + "second": { "b": [1, 2], "c": 0 ,"d":0}, + "patch": [ + { "op": "move", "from": "/b/0", "path": "/c"}, + { "op": "add", "path": "/d", "value": 0} + ] } ] \ No newline at end of file