From cc09d922394e176d1211e143f53cc8e80c12e3fe Mon Sep 17 00:00:00 2001 From: zmarois Date: Wed, 16 May 2018 14:53:53 -0400 Subject: [PATCH 1/2] treating empty object as null --- .../com/github/fge/jsonpatch/operation/TestOperation.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/fge/jsonpatch/operation/TestOperation.java b/src/main/java/com/github/fge/jsonpatch/operation/TestOperation.java index be324adc..53081a98 100644 --- a/src/main/java/com/github/fge/jsonpatch/operation/TestOperation.java +++ b/src/main/java/com/github/fge/jsonpatch/operation/TestOperation.java @@ -35,7 +35,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 @@ -61,10 +61,10 @@ 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(); From 0b04428a2253b9b394a3f0b2e71328b8114db1a9 Mon Sep 17 00:00:00 2001 From: zmarois Date: Wed, 16 May 2018 14:54:50 -0400 Subject: [PATCH 2/2] Adding tests for treating empty object as null --- src/test/resources/jsonpatch/standard/test.json | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/test/resources/jsonpatch/standard/test.json b/src/test/resources/jsonpatch/standard/test.json index d80f3c60..12a361ed 100644 --- a/src/test/resources/jsonpatch/standard/test.json +++ b/src/test/resources/jsonpatch/standard/test.json @@ -14,7 +14,13 @@ "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 +32,12 @@ "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 +}