Skip to content

Commit

Permalink
[json][patch] enable null value
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Mar 1, 2024
1 parent 15e06fd commit 7dfa494
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +55,7 @@ private void diff(final List<JsonPatchOperation> patchBuilder, final String base
diffJsonObjects(patchBuilder, basePath + "/", (Map<String, ?>) src, (Map<String, ?>) 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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down

0 comments on commit 7dfa494

Please sign in to comment.