Skip to content

Commit

Permalink
fix evaluation of empty with wrapping providers (fixes #366)
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenberger committed Jun 29, 2017
1 parent 720cf09 commit d5acf25
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,20 +359,19 @@ public Object getJson() {
}

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,35 @@ public void test_type_ref_fail() throws IOException {

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<T> {
public T gen;
Expand Down

0 comments on commit d5acf25

Please sign in to comment.