From 2611195fd693c8d34385b00973a54aa4b9212efa Mon Sep 17 00:00:00 2001 From: "Douglas, Dan" Date: Mon, 5 Feb 2018 16:24:56 -0800 Subject: [PATCH] Update to pickup change in zjsonpatch 0.4.2 Closes https://github.com/eBay/bsonpatch/issues/2 --- README.md | 4 +-- pom.xml | 2 +- .../ebay/bsonpatch/InPlaceApplyProcessor.java | 27 ++++++++++++++++++- .../java/com/ebay/bsonpatch/JsonDiffTest.java | 11 ++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 171552c..03bc142 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ The code here was ported (copied, renamed, repackaged, modified) from the [zjson ### How to use: -### Current Version : 0.4.1 +### Current Version : 0.4.2 Add following to `` section of your pom.xml - @@ -28,7 +28,7 @@ Add following to `` section of your pom.xml - com.ebay.bsonpatch bsonpatch - 0.4.1 + 0.4.2 ``` diff --git a/pom.xml b/pom.xml index 2c90769..1535855 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.ebay.bsonpatch bsonpatch - 0.4.1-SNAPSHOT + 0.4.2 jar ${project.groupId}:${project.artifactId} diff --git a/src/main/java/com/ebay/bsonpatch/InPlaceApplyProcessor.java b/src/main/java/com/ebay/bsonpatch/InPlaceApplyProcessor.java index b78aefe..e564873 100644 --- a/src/main/java/com/ebay/bsonpatch/InPlaceApplyProcessor.java +++ b/src/main/java/com/ebay/bsonpatch/InPlaceApplyProcessor.java @@ -23,7 +23,9 @@ import java.util.List; import org.bson.BsonArray; +import org.bson.BsonBinary; import org.bson.BsonDocument; +import org.bson.BsonJavaScriptWithScope; import org.bson.BsonValue; class InPlaceApplyProcessor implements BsonPatchProcessor { @@ -58,7 +60,8 @@ public void copy(List fromPath, List toPath) { BsonValue parentNode = getParentNode(fromPath, Operation.COPY); String field = fromPath.get(fromPath.size() - 1).replaceAll("\"", ""); BsonValue valueNode = parentNode.isArray() ? parentNode.asArray().get(Integer.parseInt(field)) : parentNode.asDocument().get(field); - add(toPath, valueNode); + BsonValue valueToCopy = valueNode != null ? cloneBsonValue(valueNode) : null; + add(toPath, valueToCopy); } @Override @@ -238,4 +241,26 @@ private int arrayIndex(String s, int max, boolean allowNoneExisting) { private boolean isNullOrEmpty(String string) { return string == null || string.length() == 0; } + + private static BsonValue cloneBsonValue(BsonValue from) { + BsonValue to; + switch (from.getBsonType()) { + case DOCUMENT: + to = from.asDocument().clone(); + break; + case ARRAY: + to = from.asArray().clone(); + break; + case BINARY: + to = new BsonBinary(from.asBinary().getType(), from.asBinary().getData().clone()); + break; + case JAVASCRIPT_WITH_SCOPE: + to = new BsonJavaScriptWithScope(from.asJavaScriptWithScope().getCode(), from.asJavaScriptWithScope().getScope().clone()); + break; + default: + to = from; // assume that from is immutable + } + return to; + } + } diff --git a/src/test/java/com/ebay/bsonpatch/JsonDiffTest.java b/src/test/java/com/ebay/bsonpatch/JsonDiffTest.java index 10f26cd..69a4fe4 100644 --- a/src/test/java/com/ebay/bsonpatch/JsonDiffTest.java +++ b/src/test/java/com/ebay/bsonpatch/JsonDiffTest.java @@ -140,4 +140,15 @@ public void testRenderedOperationsExceptMoveAndCopy() throws Exception { } + @Test + public void testPath() throws Exception { + BsonValue source = BsonDocument.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world\"}]}}"); + BsonArray patch = BsonArray.parse("[{\"op\":\"copy\",\"from\":\"/profiles/def/0\", \"path\":\"/profiles/def/0\"},{\"op\":\"replace\",\"path\":\"/profiles/def/0/hello\",\"value\":\"world2\"}]"); + + BsonValue target = BsonPatch.apply(patch, source); + //System.out.println(target); + BsonValue expected = BsonDocument.parse("{\"profiles\":{\"abc\":[],\"def\":[{\"hello\":\"world2\"},{\"hello\":\"world\"}]}}"); + Assert.assertTrue(target.equals(expected)); + } + }