Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding STRICT_MODE option for jsonpath filters #941

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion json-path/src/main/java/com/jayway/jsonpath/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ public enum Option {
* If REQUIRE_PROPERTIES option is present PathNotFoundException is thrown.
* If REQUIRE_PROPERTIES option is not present ["b-val"] is returned.
*/
REQUIRE_PROPERTIES
REQUIRE_PROPERTIES,

/**
* Configures JsonPath to require all properties defined in filters.
*
*
* Given:
*
* <pre>
* [
* {
* "a" : "a-val",
* "b" : "b-val"
* },
* {
* "a" : "a-val",
* }
* ]
* </pre>
*
* evaluating the filter "[?(@['does_not_exist'] NIN ['1', '2'])]"
*
* If STRICT_MODE option is present, filter will evaluate to false.
* If STRICT_MODE option is not present, filter will evaluate to true.
*/
STRICT_MODE

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jayway.jsonpath.internal.filter;

import com.jayway.jsonpath.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -39,6 +40,9 @@ public boolean apply(PredicateContext ctx) {
if(right.isPathNode()){
r = right.asPathNode().evaluate(ctx);
}
if (ctx.configuration().containsOption(Option.STRICT_MODE) && (l.isUndefinedNode() || r.isUndefinedNode())) {
return false;
}
Evaluator evaluator = EvaluatorFactory.createEvaluator(relationalOperator);
if(evaluator != null){
return evaluator.evaluate(l, r, ctx);
Expand Down
4 changes: 2 additions & 2 deletions json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public class BaseTest {
" \"expensive\": 10\n" +
"}";

public Predicate.PredicateContext createPredicateContext(final Object check) {
public Predicate.PredicateContext createPredicateContext(final Object check, Option... options) {

return new PredicateContextImpl(check, check, Configuration.defaultConfiguration(), new HashMap<Path, Object>());
return new PredicateContextImpl(check, check, Configuration.defaultConfiguration().setOptions(options), new HashMap<Path, Object>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.spi.json.JsonProvider;
import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -168,6 +169,8 @@ public void nin_filters_evaluates() throws Exception {

assertFalse(filter(where("item").nin(3)).apply(createPredicateContext(check)));
assertFalse(filter(where("item").nin(asList(3))).apply(createPredicateContext(check)));
assertFalse(filter(where("not_existent_item").nin(3)).apply(createPredicateContext(check, Option.STRICT_MODE)));
assertFalse(filter(where("not_existent_item").nin(asList(3))).apply(createPredicateContext(check, Option.STRICT_MODE)));
}

@Test
Expand Down