Skip to content

Commit

Permalink
error handling # representing invalid path as json path instead of array
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwakarma committed Jul 26, 2017
1 parent e7f2500 commit d27b756
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/main/java/com/flipkart/zjsonpatch/InPlaceApplyProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;

import java.util.List;

Expand All @@ -35,6 +38,16 @@ public JsonNode result() {
return target;
}

private static final EncodePathFunction ENCODE_PATH_FUNCTION = new EncodePathFunction();

private final static class EncodePathFunction implements Function<Object, String> {
@Override
public String apply(Object object) {
String path = object.toString(); // see http://tools.ietf.org/html/rfc6901#section-4
return path.replaceAll("~", "~0").replaceAll("/", "~1");
}
}

@Override
public void move(List<String> fromPath, List<String> toPath) {
JsonNode parentNode = getParentNode(fromPath, Operation.MOVE);
Expand Down Expand Up @@ -66,7 +79,7 @@ public void test(List<String> path, JsonNode value) {
error(Operation.TEST, "value mismatch");
}
else if (!parentNode.isContainerNode())
error(Operation.TEST, "parent is not a container in source, path provided : " + path + " | node : " + parentNode);
error(Operation.TEST, "parent is not a container in source, path provided : " + getArrayNodeRepresentation(path) + " | node : " + parentNode);
else if (parentNode.isArray()) {
final ArrayNode target = (ArrayNode) parentNode;
String idxStr = path.get(path.size() - 1);
Expand All @@ -88,7 +101,7 @@ else if (parentNode.isArray()) {
String key = path.get(path.size() - 1).replaceAll("\"", "");
JsonNode actual = target.get(key);
if (actual == null)
error(Operation.TEST, "noSuchPath in source, path provided : " + path);
error(Operation.TEST, "noSuchPath in source, path provided : " + getArrayNodeRepresentation(path));
else if (!actual.equals(value))
error(Operation.TEST, "value mismatch");
}
Expand All @@ -105,7 +118,7 @@ public void add(List<String> path, JsonNode value) {
if (fieldToReplace.equals("") && path.size() == 1)
target = value;
else if (!parentNode.isContainerNode())
error(Operation.ADD, "parent is not a container in source, path provided : " + path + " | node : " + parentNode);
error(Operation.ADD, "parent is not a container in source, path provided : " + getArrayNodeRepresentation(path) + " | node : " + parentNode);
else if (parentNode.isArray())
addToArray(path, value, parentNode);
else
Expand Down Expand Up @@ -146,7 +159,7 @@ else if (parentNode.isObject())
else if (parentNode.isArray())
((ArrayNode) parentNode).set(arrayIndex(fieldToReplace, parentNode.size() - 1), value);
else
error(Operation.REPLACE, "noSuchPath in source, path provided : " + path);
error(Operation.REPLACE, "noSuchPath in source, path provided : " + getArrayNodeRepresentation(path));
}
}

Expand All @@ -162,7 +175,7 @@ public void remove(List<String> path) {
else if (parentNode.isArray())
((ArrayNode) parentNode).remove(arrayIndex(fieldToRemove, parentNode.size() - 1));
else
error(Operation.REMOVE, "noSuchPath in source, path provided : " + path);
error(Operation.REMOVE, "noSuchPath in source, path provided : " + getArrayNodeRepresentation(path));
}
}

Expand All @@ -173,7 +186,7 @@ private void error(Operation forOp, String message) {
private JsonNode getParentNode(List<String> fromPath, Operation forOp) {
List<String> pathToParent = fromPath.subList(0, fromPath.size() - 1); // would never by out of bound, lets see
JsonNode node = getNode(target, pathToParent, 1);
if (node == null) error(forOp, "noSuchPath in source, path provided: " + fromPath);
if (node == null) error(forOp, "noSuchPath in source, path provided: " + getArrayNodeRepresentation(fromPath));
return node;
}

Expand Down Expand Up @@ -213,4 +226,8 @@ private int arrayIndex(String s, int max) {
}
return index;
}
private static String getArrayNodeRepresentation(List<String> path) {
return Joiner.on('/').appendTo(new StringBuilder().append('/'),
Iterables.transform(path, ENCODE_PATH_FUNCTION)).toString();
}
}

0 comments on commit d27b756

Please sign in to comment.