From d5acf25b9f9ee43e88fe76b7cdb866f963026e30 Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Thu, 29 Jun 2017 14:28:50 +0200 Subject: [PATCH] fix evaluation of `empty` with wrapping providers (fixes #366) --- .../jsonpath/internal/filter/ValueNode.java | 9 +++--- .../JacksonJsonNodeJsonProviderTest.java | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java index 08d257c4..9989dd95 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java @@ -359,20 +359,19 @@ public abstract class ValueNode { } public boolean isArray(Predicate.PredicateContext ctx) { - return parse(ctx) instanceof List; + return ctx.configuration().jsonProvider().isArray(parse(ctx)); } public boolean isMap(Predicate.PredicateContext ctx) { - return parse(ctx) instanceof Map; + return ctx.configuration().jsonProvider().isMap(parse(ctx)); } public int length(Predicate.PredicateContext ctx) { - return isArray(ctx) ? ((List) parse(ctx)).size() : -1; + return isArray(ctx) ? ctx.configuration().jsonProvider().length(parse(ctx)) : -1; } public boolean isEmpty(Predicate.PredicateContext ctx) { - if (isArray(ctx)) return ((List) parse(ctx)).size() == 0; - else if (isMap(ctx)) return ((Map) parse(ctx)).size() == 0; + if (isArray(ctx) || isMap(ctx)) return ctx.configuration().jsonProvider().length(parse(ctx)) == 0; else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0; return true; } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java b/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java index 09401682..d58117bd 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java @@ -109,6 +109,35 @@ public class JacksonJsonNodeJsonProviderTest extends BaseTest { using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef); } + + @Test + // https://github.com/json-path/JsonPath/issues/366 + public void empty_array_check_works() throws IOException { + String json = "[" + + " {" + + " \"name\": \"a\"," + + " \"groups\": [{" + + " \"type\": \"phase\"," + + " \"name\": \"alpha\"" + + " }, {" + + " \"type\": \"not_phase\"," + + " \"name\": \"beta\"" + + " }]" + + " }, {" + + " \"name\": \"b\"," + + " \"groups\": [{" + + " \"type\": \"phase\"," + + " \"name\": \"beta\"" + + " }, {" + + " \"type\": \"not_phase\"," + + " \"name\": \"alpha\"" + + " }]" + + " }" + + "]"; + ArrayNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(json).read("$[?(@.groups[?(@.type == 'phase' && @.name == 'alpha')] empty false)]"); + assertThat(node.size()).isEqualTo(1); + assertThat(node.get(0).get("name").asText()).isEqualTo("a"); + } public static class FooBarBaz { public T gen;