From 12ca3d90aecb652de29ed1680e0a5b3d49a12df4 Mon Sep 17 00:00:00 2001 From: Zachary Marois Date: Thu, 19 Jan 2017 15:26:21 -0500 Subject: [PATCH] Enabling testing for a pointer that references a nonexistent value explicitly being null --- .../java/com/github/fge/jsonpatch/TestOperation.java | 7 ++++--- src/test/resources/jsonpatch/test.json | 12 +++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/fge/jsonpatch/TestOperation.java b/src/main/java/com/github/fge/jsonpatch/TestOperation.java index c6b3fa54..ff38a9df 100644 --- a/src/main/java/com/github/fge/jsonpatch/TestOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/TestOperation.java @@ -33,7 +33,7 @@ * to test ({@code path}) and the value to test equality against ({@code * value}).

* - *

It is an error if no value exists at the given path.

+ *

It is an error if no value exists at the given path and there is an expected value to test equality against.

* *

Also note that equality as defined by JSON Patch is exactly the same as it * is defined by JSON Schema itself. As such, this operation reuses {@link @@ -57,10 +57,11 @@ public JsonNode apply(final JsonNode node) throws JsonPatchException { final JsonNode tested = path.path(node); - if (tested.isMissingNode()) + + if (tested.isMissingNode() && !value.isNull()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); - if (!EQUIVALENCE.equivalent(tested, value)) + if (!(tested.isMissingNode() && value.isNull()) && !EQUIVALENCE.equivalent(tested, value)) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.valueTestFailure")); return node.deepCopy(); diff --git a/src/test/resources/jsonpatch/test.json b/src/test/resources/jsonpatch/test.json index d80f3c60..6ce1d210 100644 --- a/src/test/resources/jsonpatch/test.json +++ b/src/test/resources/jsonpatch/test.json @@ -14,6 +14,11 @@ "op": { "op": "test", "path": "/x", "value": -30.000 }, "node": { "x": -29.020 }, "message": "jsonPatch.valueTestFailure" + }, + { + "op": { "op": "test", "path": "/a/1", "value": null }, + "node": { "a": [ null, "hello", "world" ] }, + "message": "jsonPatch.valueTestFailure" } ], "ops": [ @@ -26,6 +31,11 @@ "op": { "op": "test", "path": "/a/1", "value": "hello" }, "node": { "a": [ null, "hello", "world" ] }, "expected": { "a": [ null, "hello", "world" ] } + }, + { + "op": { "op": "test", "path": "/x", "value": null }, + "node": 1.00, + "expected": 1.00 } ] -} \ No newline at end of file +}