diff --git a/fusion-json/src/main/java/io/yupiik/fusion/json/diff/GenericJsonDiff.java b/fusion-json/src/main/java/io/yupiik/fusion/json/diff/GenericJsonDiff.java index 7f48541e..2cff4710 100644 --- a/fusion-json/src/main/java/io/yupiik/fusion/json/diff/GenericJsonDiff.java +++ b/fusion-json/src/main/java/io/yupiik/fusion/json/diff/GenericJsonDiff.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Stream; import static io.yupiik.fusion.json.patch.JsonPatchOperation.Operation.add; @@ -54,7 +55,7 @@ private void diff(final List patchBuilder, final String base diffJsonObjects(patchBuilder, basePath + "/", (Map) src, (Map) tg); } else if (source instanceof List l1 && target instanceof List l2) { diffJsonArray(patchBuilder, basePath + "/", l1, l2); - } else if (!source.equals(target)) { + } else if (!Objects.equals(source, target)) { patchBuilder.add(new JsonPatchOperation(replace, basePath, null, target)); } } diff --git a/fusion-json/src/test/java/io/yupiik/fusion/json/diff/GenericJsonDiffTest.java b/fusion-json/src/test/java/io/yupiik/fusion/json/diff/GenericJsonDiffTest.java index d7b2ea2a..874c3cd8 100644 --- a/fusion-json/src/test/java/io/yupiik/fusion/json/diff/GenericJsonDiffTest.java +++ b/fusion-json/src/test/java/io/yupiik/fusion/json/diff/GenericJsonDiffTest.java @@ -20,6 +20,8 @@ import org.junit.jupiter.api.Test; import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -28,8 +30,22 @@ import static io.yupiik.fusion.json.patch.JsonPatchOperation.Operation.remove; import static io.yupiik.fusion.json.patch.JsonPatchOperation.Operation.replace; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; class GenericJsonDiffTest { + @Test + public void nullDiff() { + final var list = new ArrayList<>(); + list.add(null); + final var patch = List.of(new JsonPatchOperation(add, "/testEmpty/0", null, null)); + final var target = Map.of("testEmpty", list); + final var from = Map.of("testEmpty", List.of()); + assertDiff(from, target, patch); + final var patched = new GenericJsonPatch(patch).apply(from); + assertEquals(target, patched); + assertNotSame(patched, target); + } + @Test public void fromEmptyArray() { assertDiff(