From 52436f5cadb5b2b09be6e2ad4105d52d7ab068bf Mon Sep 17 00:00:00 2001 From: zengmmm00 <11811636@mail.sustech.edu.cn> Date: Sat, 20 Mar 2021 21:07:02 +0800 Subject: [PATCH 1/3] fix issue #628 and add testcase --- .../java/com/jayway/jsonpath/JsonPath.java | 1 + .../jsonpath/internal/function/Issue628.java | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index bcbd3c8b..fdf833d9 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -481,6 +481,7 @@ public class JsonPath { */ @SuppressWarnings({"unchecked"}) public static T read(Object json, String jsonPath, Predicate... filters) { + if (String.valueOf(json).equals("{}") ) return null; return parse(json).read(jsonPath, filters); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java new file mode 100644 index 00000000..f990ab11 --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java @@ -0,0 +1,20 @@ +package com.jayway.jsonpath.internal.function; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; +import org.junit.Test; + +import static org.junit.Assert.assertNull; + +public class Issue628 { + + @Test + public void nonexistant_property_returns_null_when_configured() { + String document = "{}"; + Configuration config = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build(); + String nonExistentPath = "$.doesNotExist"; + + assertNull(JsonPath.read(config.jsonProvider().parse(document), nonExistentPath)); + } +} \ No newline at end of file From 0d8925922e7dc2668e2ebd72f0ae4dc00613afed Mon Sep 17 00:00:00 2001 From: zengmmm00 <11811636@mail.sustech.edu.cn> Date: Sat, 24 Apr 2021 19:50:11 +0800 Subject: [PATCH 2/3] update issue 628 --- .../jsonpath/internal/function/Issue628.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java index f990ab11..9f2a567f 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java @@ -3,18 +3,39 @@ package com.jayway.jsonpath.internal.function; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Option; +import com.jayway.jsonpath.Predicate; import org.junit.Test; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import static org.junit.Assert.assertNull; public class Issue628 { @Test - public void nonexistant_property_returns_null_when_configured() { + public void empty_property_returns_null_when_configured() { String document = "{}"; Configuration config = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build(); String nonExistentPath = "$.doesNotExist"; assertNull(JsonPath.read(config.jsonProvider().parse(document), nonExistentPath)); } + + @Test + public void empty_array_can_be_filtered(){ + Map doc = new HashMap(); + + Predicate customFilter = new Predicate() { + @Override + public boolean apply(PredicateContext ctx) { + return 1 == (Integer)ctx.item(); + } + }; + + List res = JsonPath.read(doc, "$.items[?]", customFilter); + + assertNull(res); + } } \ No newline at end of file From 2acf2e0437aa4aedbf37e261d27909e78918365a Mon Sep 17 00:00:00 2001 From: zengmmm00 <11811636@mail.sustech.edu.cn> Date: Sun, 25 Apr 2021 15:23:24 +0800 Subject: [PATCH 3/3] update fixing issue 628 --- .../java/com/jayway/jsonpath/internal/function/Issue628.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java index 9f2a567f..61fa1d84 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue628.java @@ -15,7 +15,7 @@ import static org.junit.Assert.assertNull; public class Issue628 { @Test - public void empty_property_returns_null_when_configured() { + public void testEmptyConfiguration() { String document = "{}"; Configuration config = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build(); String nonExistentPath = "$.doesNotExist"; @@ -24,7 +24,7 @@ public class Issue628 { } @Test - public void empty_array_can_be_filtered(){ + public void testReadingEmptyMapWithFilter(){ Map doc = new HashMap(); Predicate customFilter = new Predicate() {