Skip to content

Commit d5acf25

Browse files
committed
fix evaluation of empty with wrapping providers (fixes json-path#366)
1 parent 720cf09 commit d5acf25

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,20 +359,19 @@ public Object getJson() {
359359
}
360360

361361
public boolean isArray(Predicate.PredicateContext ctx) {
362-
return parse(ctx) instanceof List;
362+
return ctx.configuration().jsonProvider().isArray(parse(ctx));
363363
}
364364

365365
public boolean isMap(Predicate.PredicateContext ctx) {
366-
return parse(ctx) instanceof Map;
366+
return ctx.configuration().jsonProvider().isMap(parse(ctx));
367367
}
368368

369369
public int length(Predicate.PredicateContext ctx) {
370-
return isArray(ctx) ? ((List) parse(ctx)).size() : -1;
370+
return isArray(ctx) ? ctx.configuration().jsonProvider().length(parse(ctx)) : -1;
371371
}
372372

373373
public boolean isEmpty(Predicate.PredicateContext ctx) {
374-
if (isArray(ctx)) return ((List) parse(ctx)).size() == 0;
375-
else if (isMap(ctx)) return ((Map) parse(ctx)).size() == 0;
374+
if (isArray(ctx) || isMap(ctx)) return ctx.configuration().jsonProvider().length(parse(ctx)) == 0;
376375
else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0;
377376
return true;
378377
}

json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,35 @@ public void test_type_ref_fail() throws IOException {
109109

110110
using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef);
111111
}
112+
113+
@Test
114+
// https://github.com/json-path/JsonPath/issues/366
115+
public void empty_array_check_works() throws IOException {
116+
String json = "[" +
117+
" {" +
118+
" \"name\": \"a\"," +
119+
" \"groups\": [{" +
120+
" \"type\": \"phase\"," +
121+
" \"name\": \"alpha\"" +
122+
" }, {" +
123+
" \"type\": \"not_phase\"," +
124+
" \"name\": \"beta\"" +
125+
" }]" +
126+
" }, {" +
127+
" \"name\": \"b\"," +
128+
" \"groups\": [{" +
129+
" \"type\": \"phase\"," +
130+
" \"name\": \"beta\"" +
131+
" }, {" +
132+
" \"type\": \"not_phase\"," +
133+
" \"name\": \"alpha\"" +
134+
" }]" +
135+
" }" +
136+
"]";
137+
ArrayNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(json).read("$[?(@.groups[?(@.type == 'phase' && @.name == 'alpha')] empty false)]");
138+
assertThat(node.size()).isEqualTo(1);
139+
assertThat(node.get(0).get("name").asText()).isEqualTo("a");
140+
}
112141

113142
public static class FooBarBaz<T> {
114143
public T gen;

0 commit comments

Comments
 (0)