From 8f5df926a4e77ec36713490c6f216476cb19d5ab Mon Sep 17 00:00:00 2001 From: Ram Nukala Date: Tue, 6 Jul 2021 14:35:24 -0500 Subject: [PATCH 1/2] work in progress --- build.gradle | 4 ++-- .../github/fge/jsonpatch/AddOperation.java | 23 +++++++++++++------ .../github/fge/jsonpatch/CopyOperation.java | 10 +++++--- .../github/fge/jsonpatch/MoveOperation.java | 10 +++++--- .../github/fge/jsonpatch/RemoveOperation.java | 9 ++++++-- .../fge/jsonpatch/ReplaceOperation.java | 10 +++++--- .../github/fge/jsonpatch/TestOperation.java | 4 ++-- .../fge/jsonpatch/JsonPatchOperationTest.java | 22 +++++++++++++++++- 8 files changed, 69 insertions(+), 23 deletions(-) diff --git a/build.gradle b/build.gradle index 8ffa1a34..39960a15 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ buildscript { repositories { mavenCentral() maven { - url "http://repo.springsource.org/plugins-release"; + url "https://repo.springsource.org/plugins-release"; } } dependencies { @@ -31,7 +31,7 @@ buildscript { }; plugins { - id("net.ltgt.errorprone") version "0.8.1" apply false + id("net.ltgt.errorprone") version "1.3.0" apply false } configure(allprojects) { diff --git a/src/main/java/com/github/fge/jsonpatch/AddOperation.java b/src/main/java/com/github/fge/jsonpatch/AddOperation.java index 5e5fb57a..2b104fde 100644 --- a/src/main/java/com/github/fge/jsonpatch/AddOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/AddOperation.java @@ -91,11 +91,20 @@ public JsonNode apply(final JsonNode node) */ final JsonNode parentNode = path.parent().path(node); if (parentNode.isMissingNode()) - throw new JsonPatchException(BUNDLE.getMessage( - "jsonPatch.noSuchParent")); - if (!parentNode.isContainerNode()) - throw new JsonPatchException(BUNDLE.getMessage( - "jsonPatch.parentNotContainer")); + { + String msg = BUNDLE.getMessage("jsonPatch.noSuchParent"); + if(node!=null) { + msg = node.toString() + " " + msg; + } + throw new JsonPatchException(msg); + } + if (!parentNode.isContainerNode()) { + String msg = BUNDLE.getMessage("jsonPatch.parentNotContainer"); + if(node!=null) { + msg = node.toString() + " " + msg; + } + throw new JsonPatchException(msg); + } return parentNode.isArray() ? addToArray(path, node) : addToObject(path, node); @@ -119,12 +128,12 @@ private JsonNode addToArray(final JsonPointer path, final JsonNode node) try { index = Integer.parseInt(token.toString()); } catch (NumberFormatException ignored) { - throw new JsonPatchException(BUNDLE.getMessage( + throw new JsonPatchException("[] " + BUNDLE.getMessage( "jsonPatch.notAnIndex")); } if (index < 0 || index > size) - throw new JsonPatchException(BUNDLE.getMessage( + throw new JsonPatchException(node.toString() + " " + BUNDLE.getMessage( "jsonPatch.noSuchIndex")); target.insert(index, value); diff --git a/src/main/java/com/github/fge/jsonpatch/CopyOperation.java b/src/main/java/com/github/fge/jsonpatch/CopyOperation.java index 9a5a75b9..bae8d837 100644 --- a/src/main/java/com/github/fge/jsonpatch/CopyOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/CopyOperation.java @@ -55,9 +55,13 @@ public JsonNode apply(final JsonNode node) throws JsonPatchException { final JsonNode dupData = from.path(node).deepCopy(); - if (dupData.isMissingNode()) - throw new JsonPatchException(BUNDLE.getMessage( - "jsonPatch.noSuchPath")); + if (path.path(dupData).isMissingNode()) { + String msg = BUNDLE.getMessage("jsonPatch.noSuchPath"); + if(dupData!=null) { + msg = node.toString() + " " + msg; + } + throw new JsonPatchException(msg); + } return new AddOperation(path, dupData).apply(node); } } diff --git a/src/main/java/com/github/fge/jsonpatch/MoveOperation.java b/src/main/java/com/github/fge/jsonpatch/MoveOperation.java index 405b737c..c8b2f031 100644 --- a/src/main/java/com/github/fge/jsonpatch/MoveOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/MoveOperation.java @@ -79,9 +79,13 @@ public JsonNode apply(final JsonNode node) if (from.equals(path)) return node.deepCopy(); final JsonNode movedNode = from.path(node); - if (movedNode.isMissingNode()) - throw new JsonPatchException(BUNDLE.getMessage( - "jsonPatch.noSuchPath")); + if (movedNode.isMissingNode()) { + String msg = BUNDLE.getMessage("jsonPatch.noSuchPath"); + if(node!=null) { + msg = node.toString() + " " + msg; + } + throw new JsonPatchException(msg); + } final JsonPatchOperation remove = new RemoveOperation(from); final JsonPatchOperation add = new AddOperation(path, movedNode); return add.apply(remove.apply(node)); diff --git a/src/main/java/com/github/fge/jsonpatch/RemoveOperation.java b/src/main/java/com/github/fge/jsonpatch/RemoveOperation.java index 4c528baa..c03436f9 100644 --- a/src/main/java/com/github/fge/jsonpatch/RemoveOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/RemoveOperation.java @@ -55,8 +55,13 @@ public JsonNode apply(final JsonNode node) if (path.isEmpty()) return MissingNode.getInstance(); if (path.path(node).isMissingNode()) - throw new JsonPatchException(BUNDLE.getMessage( - "jsonPatch.noSuchPath")); + { + String msg = BUNDLE.getMessage("jsonPatch.noSuchPath"); + if(node!=null) { + msg = node.toString() + " " + msg; + } + throw new JsonPatchException(msg); + } final JsonNode ret = node.deepCopy(); final JsonNode parentNode = path.parent().get(ret); final String raw = Iterables.getLast(path).getToken().getRaw(); diff --git a/src/main/java/com/github/fge/jsonpatch/ReplaceOperation.java b/src/main/java/com/github/fge/jsonpatch/ReplaceOperation.java index d4454bdb..72be6663 100644 --- a/src/main/java/com/github/fge/jsonpatch/ReplaceOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/ReplaceOperation.java @@ -62,9 +62,13 @@ public JsonNode apply(final JsonNode node) * If remove is done first, the array is empty and add rightly complains * that there is no such index in the array. */ - if (path.path(node).isMissingNode()) - throw new JsonPatchException(BUNDLE.getMessage( - "jsonPatch.noSuchPath")); + if (path.path(node).isMissingNode()) { + String msg = BUNDLE.getMessage("jsonPatch.noSuchPath"); + if(node!=null) { + msg = node.toString() + " " + msg; + } + throw new JsonPatchException(msg); + } final JsonNode replacement = value.deepCopy(); if (path.isEmpty()) return replacement; diff --git a/src/main/java/com/github/fge/jsonpatch/TestOperation.java b/src/main/java/com/github/fge/jsonpatch/TestOperation.java index 90f03401..a736161e 100644 --- a/src/main/java/com/github/fge/jsonpatch/TestOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/TestOperation.java @@ -57,10 +57,10 @@ public JsonNode apply(final JsonNode node) { final JsonNode tested = path.path(node); if (tested.isMissingNode()) - throw new JsonPatchException(BUNDLE.getMessage( + throw new JsonPatchException(tested.toString() + " "+ BUNDLE.getMessage( "jsonPatch.noSuchPath")); if (!EQUIVALENCE.equivalent(tested, value)) - throw new JsonPatchException(BUNDLE.getMessage( + throw new JsonPatchException(tested.toString() + " " + BUNDLE.getMessage( "jsonPatch.valueTestFailure")); return node.deepCopy(); } diff --git a/src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java b/src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java index 5f04d811..5854afb2 100644 --- a/src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java +++ b/src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java @@ -81,12 +81,27 @@ public final void errorsAreCorrectlyReported(final JsonNode patch, throws IOException { final JsonPatchOperation op = reader.readValue(patch); + System.out.println("Message is:"+ message); + if(message.equals("parent of node to add does not exist")) + { + System.out.println("Stop here"); + } try { op.apply(node); fail("No exception thrown!!"); } catch (JsonPatchException e) { - assertEquals(e.getMessage(), message); + String msg = message; + if(node!=null) { + msg = node.toString() + " " + msg; + } + String className = this.getClass().toString(); + System.out.println("ClassName is:"+ className); + if(!msg.equalsIgnoreCase(e.getMessage()) && className.contains("CopyOperationTest")) + { + System.out.println("Stop here"); + } + assertEquals(e.getMessage(), msg); } } @@ -110,6 +125,11 @@ public final void operationsYieldExpectedResults(final JsonNode patch, final JsonNode node, final JsonNode expected) throws IOException, JsonPatchException { + String className = this.getClass().toString(); + if(className.contains("CopyOperationTest")) + { + System.out.println(""); + } final JsonPatchOperation op = reader.readValue(patch); final JsonNode actual = op.apply(node); From ea807b5b19d7a016f82b43f569671acd9cb786c6 Mon Sep 17 00:00:00 2001 From: Ram Nukala Date: Tue, 6 Jul 2021 21:38:48 -0500 Subject: [PATCH 2/2] working tests --- .../github/fge/jsonpatch/CopyOperation.java | 2 +- .../github/fge/jsonpatch/MoveOperation.java | 20 ++++++++++++++++++- .../github/fge/jsonpatch/TestOperation.java | 12 +++++++---- .../fge/jsonpatch/JsonPatchOperationTest.java | 17 ---------------- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/github/fge/jsonpatch/CopyOperation.java b/src/main/java/com/github/fge/jsonpatch/CopyOperation.java index bae8d837..5e5e8f5d 100644 --- a/src/main/java/com/github/fge/jsonpatch/CopyOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/CopyOperation.java @@ -55,7 +55,7 @@ public JsonNode apply(final JsonNode node) throws JsonPatchException { final JsonNode dupData = from.path(node).deepCopy(); - if (path.path(dupData).isMissingNode()) { + if(dupData.isMissingNode()){ String msg = BUNDLE.getMessage("jsonPatch.noSuchPath"); if(dupData!=null) { msg = node.toString() + " " + msg; diff --git a/src/main/java/com/github/fge/jsonpatch/MoveOperation.java b/src/main/java/com/github/fge/jsonpatch/MoveOperation.java index c8b2f031..b09e7add 100644 --- a/src/main/java/com/github/fge/jsonpatch/MoveOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/MoveOperation.java @@ -88,6 +88,24 @@ public JsonNode apply(final JsonNode node) } final JsonPatchOperation remove = new RemoveOperation(from); final JsonPatchOperation add = new AddOperation(path, movedNode); - return add.apply(remove.apply(node)); + JsonNode returnNode = null; + try { + returnNode = add.apply(remove.apply(node)); + } + catch(JsonPatchException jpe) + { + String msg = BUNDLE.getMessage("jsonPatch.noSuchParent"); + if(jpe.getMessage().contains(msg)) + { + // Rethrow the JsonPatchException with a new message + msg = node.toString() + " " + msg; + throw new JsonPatchException(msg); + } + else + { + throw jpe; + } + } + return returnNode; } } diff --git a/src/main/java/com/github/fge/jsonpatch/TestOperation.java b/src/main/java/com/github/fge/jsonpatch/TestOperation.java index a736161e..c5794ade 100644 --- a/src/main/java/com/github/fge/jsonpatch/TestOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/TestOperation.java @@ -56,11 +56,15 @@ public JsonNode apply(final JsonNode node) throws JsonPatchException { final JsonNode tested = path.path(node); - if (tested.isMissingNode()) - throw new JsonPatchException(tested.toString() + " "+ BUNDLE.getMessage( - "jsonPatch.noSuchPath")); + if (tested.isMissingNode()) { + String msg = BUNDLE.getMessage("jsonPatch.noSuchPath"); + if(node!=null) { + msg = node.toString() + " " + msg; + } + throw new JsonPatchException(msg); + } if (!EQUIVALENCE.equivalent(tested, value)) - throw new JsonPatchException(tested.toString() + " " + BUNDLE.getMessage( + throw new JsonPatchException(node.toString() + " " + BUNDLE.getMessage( "jsonPatch.valueTestFailure")); return node.deepCopy(); } diff --git a/src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java b/src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java index 5854afb2..90b98c95 100644 --- a/src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java +++ b/src/test/java/com/github/fge/jsonpatch/JsonPatchOperationTest.java @@ -81,12 +81,6 @@ public final void errorsAreCorrectlyReported(final JsonNode patch, throws IOException { final JsonPatchOperation op = reader.readValue(patch); - System.out.println("Message is:"+ message); - if(message.equals("parent of node to add does not exist")) - { - System.out.println("Stop here"); - } - try { op.apply(node); fail("No exception thrown!!"); @@ -95,12 +89,6 @@ public final void errorsAreCorrectlyReported(final JsonNode patch, if(node!=null) { msg = node.toString() + " " + msg; } - String className = this.getClass().toString(); - System.out.println("ClassName is:"+ className); - if(!msg.equalsIgnoreCase(e.getMessage()) && className.contains("CopyOperationTest")) - { - System.out.println("Stop here"); - } assertEquals(e.getMessage(), msg); } } @@ -125,11 +113,6 @@ public final void operationsYieldExpectedResults(final JsonNode patch, final JsonNode node, final JsonNode expected) throws IOException, JsonPatchException { - String className = this.getClass().toString(); - if(className.contains("CopyOperationTest")) - { - System.out.println(""); - } final JsonPatchOperation op = reader.readValue(patch); final JsonNode actual = op.apply(node);