Skip to content

Commit 9c5b1f3

Browse files
mbolgariwvishwakarma
authored andcommitted
Add compatibility flag for ignoring missing source node in replace operation (#102)
1 parent 797278e commit 9c5b1f3

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/main/java/com/flipkart/zjsonpatch/CompatibilityFlags.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
*/
2424
public enum CompatibilityFlags {
2525
MISSING_VALUES_AS_NULLS,
26-
REMOVE_NONE_EXISTING_ARRAY_ELEMENT;
26+
REMOVE_NONE_EXISTING_ARRAY_ELEMENT,
27+
ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE;
2728

2829
public static EnumSet<CompatibilityFlags> defaults() {
2930
return EnumSet.noneOf(CompatibilityFlags.class);

src/main/java/com/flipkart/zjsonpatch/InPlaceApplyProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public void replace(JsonPointer path, JsonNode value) throws JsonPointerEvaluati
8888
JsonNode parentNode = path.getParent().evaluate(target);
8989
JsonPointer.RefToken token = path.last();
9090
if (parentNode.isObject()) {
91-
if (!parentNode.has(token.getField()))
91+
if (!flags.contains(CompatibilityFlags.ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE) &&
92+
!parentNode.has(token.getField()))
9293
throw new JsonPatchApplicationException(
9394
"Missing field \"" + token.getField() + "\"", Operation.REPLACE, path.getParent());
9495
((ObjectNode) parentNode).replace(token.getField(), value);

src/test/java/com/flipkart/zjsonpatch/CompatibilityTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import java.io.IOException;
2525
import java.util.EnumSet;
2626

27-
import static com.flipkart.zjsonpatch.CompatibilityFlags.MISSING_VALUES_AS_NULLS;
28-
import static com.flipkart.zjsonpatch.CompatibilityFlags.REMOVE_NONE_EXISTING_ARRAY_ELEMENT;
27+
import static com.flipkart.zjsonpatch.CompatibilityFlags.*;
2928
import static org.hamcrest.core.IsEqual.equalTo;
3029
import static org.junit.Assert.assertThat;
3130

@@ -35,13 +34,15 @@ public class CompatibilityTest {
3534
JsonNode addNodeWithMissingValue;
3635
JsonNode replaceNodeWithMissingValue;
3736
JsonNode removeNoneExistingArrayElement;
37+
JsonNode replaceNode;
3838

3939
@Before
4040
public void setUp() throws Exception {
4141
mapper = new ObjectMapper();
4242
addNodeWithMissingValue = mapper.readTree("[{\"op\":\"add\",\"path\":\"/a\"}]");
4343
replaceNodeWithMissingValue = mapper.readTree("[{\"op\":\"replace\",\"path\":\"/a\"}]");
4444
removeNoneExistingArrayElement = mapper.readTree("[{\"op\": \"remove\",\"path\": \"/b/0\"}]");
45+
replaceNode = mapper.readTree("[{\"op\":\"replace\",\"path\":\"/a\",\"value\":true}]");
4546
}
4647

4748
@Test
@@ -76,4 +77,11 @@ public void withFlagIgnoreRemoveNoneExistingArrayElement() throws IOException {
7677
JsonNode result = JsonPatch.apply(removeNoneExistingArrayElement, source, EnumSet.of(REMOVE_NONE_EXISTING_ARRAY_ELEMENT));
7778
assertThat(result, equalTo(expected));
7879
}
80+
81+
@Test
82+
public void withFlagReplaceShouldAddValueWhenMissingInTarget() throws Exception {
83+
JsonNode expected = mapper.readTree("{\"a\": true}");
84+
JsonNode result = JsonPatch.apply(replaceNode, mapper.createObjectNode(), EnumSet.of(ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE));
85+
assertThat(result, equalTo(expected));
86+
}
7987
}

0 commit comments

Comments
 (0)