Skip to content

Commit

Permalink
Add compatibility flag for ignoring missing source node in replace op…
Browse files Browse the repository at this point in the history
…eration (#102)
  • Loading branch information
mbolgariw authored and vishwakarma committed Jul 20, 2019
1 parent 797278e commit 9c5b1f3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
*/
public enum CompatibilityFlags {
MISSING_VALUES_AS_NULLS,
REMOVE_NONE_EXISTING_ARRAY_ELEMENT;
REMOVE_NONE_EXISTING_ARRAY_ELEMENT,
ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE;

public static EnumSet<CompatibilityFlags> defaults() {
return EnumSet.noneOf(CompatibilityFlags.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public void replace(JsonPointer path, JsonNode value) throws JsonPointerEvaluati
JsonNode parentNode = path.getParent().evaluate(target);
JsonPointer.RefToken token = path.last();
if (parentNode.isObject()) {
if (!parentNode.has(token.getField()))
if (!flags.contains(CompatibilityFlags.ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE) &&
!parentNode.has(token.getField()))
throw new JsonPatchApplicationException(
"Missing field \"" + token.getField() + "\"", Operation.REPLACE, path.getParent());
((ObjectNode) parentNode).replace(token.getField(), value);
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/com/flipkart/zjsonpatch/CompatibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import java.io.IOException;
import java.util.EnumSet;

import static com.flipkart.zjsonpatch.CompatibilityFlags.MISSING_VALUES_AS_NULLS;
import static com.flipkart.zjsonpatch.CompatibilityFlags.REMOVE_NONE_EXISTING_ARRAY_ELEMENT;
import static com.flipkart.zjsonpatch.CompatibilityFlags.*;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

Expand All @@ -35,13 +34,15 @@ public class CompatibilityTest {
JsonNode addNodeWithMissingValue;
JsonNode replaceNodeWithMissingValue;
JsonNode removeNoneExistingArrayElement;
JsonNode replaceNode;

@Before
public void setUp() throws Exception {
mapper = new ObjectMapper();
addNodeWithMissingValue = mapper.readTree("[{\"op\":\"add\",\"path\":\"/a\"}]");
replaceNodeWithMissingValue = mapper.readTree("[{\"op\":\"replace\",\"path\":\"/a\"}]");
removeNoneExistingArrayElement = mapper.readTree("[{\"op\": \"remove\",\"path\": \"/b/0\"}]");
replaceNode = mapper.readTree("[{\"op\":\"replace\",\"path\":\"/a\",\"value\":true}]");
}

@Test
Expand Down Expand Up @@ -76,4 +77,11 @@ public void withFlagIgnoreRemoveNoneExistingArrayElement() throws IOException {
JsonNode result = JsonPatch.apply(removeNoneExistingArrayElement, source, EnumSet.of(REMOVE_NONE_EXISTING_ARRAY_ELEMENT));
assertThat(result, equalTo(expected));
}

@Test
public void withFlagReplaceShouldAddValueWhenMissingInTarget() throws Exception {
JsonNode expected = mapper.readTree("{\"a\": true}");
JsonNode result = JsonPatch.apply(replaceNode, mapper.createObjectNode(), EnumSet.of(ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE));
assertThat(result, equalTo(expected));
}
}

0 comments on commit 9c5b1f3

Please sign in to comment.