From 6202f6b92baaeffc1e75d71c95936599338b7e1a Mon Sep 17 00:00:00 2001 From: Mythic Date: Wed, 28 Jun 2023 20:52:54 +0300 Subject: [PATCH 1/3] Adding STRICT_MODE OPTION to jsonpath filters --- .../main/java/com/jayway/jsonpath/Option.java | 27 ++++++++++++++++++- .../filter/RelationalExpressionNode.java | 4 +++ .../java/com/jayway/jsonpath/BaseTest.java | 4 +-- .../com/jayway/jsonpath/old/FilterTest.java | 24 ++++++++--------- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/Option.java b/json-path/src/main/java/com/jayway/jsonpath/Option.java index 412703fd..1d98d20b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Option.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Option.java @@ -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: + * + *
+     * [
+     *     {
+     *         "a" : "a-val",
+     *         "b" : "b-val"
+     *     },
+     *     {
+     *         "a" : "a-val",
+     *     }
+     * ]
+     * 
+ * + * 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 } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalExpressionNode.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalExpressionNode.java index 05d8adb5..3b50c17b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalExpressionNode.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalExpressionNode.java @@ -1,5 +1,6 @@ package com.jayway.jsonpath.internal.filter; +import com.jayway.jsonpath.Option; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +40,9 @@ public class RelationalExpressionNode extends ExpressionNode { 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); diff --git a/json-path/src/test/java/com/jayway/jsonpath/BaseTest.java b/json-path/src/test/java/com/jayway/jsonpath/BaseTest.java index eac44cdb..9d549ad5 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/BaseTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/BaseTest.java @@ -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()); + return new PredicateContextImpl(check, check, Configuration.defaultConfiguration().setOptions(options), new HashMap()); } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java b/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java index b956a48c..62b4f6b8 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java @@ -1,10 +1,6 @@ package com.jayway.jsonpath.old; -import com.jayway.jsonpath.BaseTest; -import com.jayway.jsonpath.Configuration; -import com.jayway.jsonpath.Filter; -import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.Predicate; +import com.jayway.jsonpath.*; import com.jayway.jsonpath.spi.json.JsonProvider; import org.assertj.core.api.Assertions; import org.junit.Test; @@ -160,14 +156,16 @@ public class FilterTest extends BaseTest { check.put("item", 3); check.put("null_item", null); - assertTrue(filter(where("item").nin(4, 5)).apply(createPredicateContext(check))); - assertTrue(filter(where("item").nin(asList(4, 5))).apply(createPredicateContext(check))); - assertTrue(filter(where("item").nin(asList('A'))).apply(createPredicateContext(check))); - assertTrue(filter(where("null_item").nin(1, 2, 3)).apply(createPredicateContext(check))); - assertTrue(filter(where("item").nin(asList((Object) null))).apply(createPredicateContext(check))); - - assertFalse(filter(where("item").nin(3)).apply(createPredicateContext(check))); - assertFalse(filter(where("item").nin(asList(3))).apply(createPredicateContext(check))); +// assertTrue(filter(where("item").nin(4, 5)).apply(createPredicateContext(check))); +// assertTrue(filter(where("item").nin(asList(4, 5))).apply(createPredicateContext(check))); +// assertTrue(filter(where("item").nin(asList('A'))).apply(createPredicateContext(check))); +// assertTrue(filter(where("null_item").nin(1, 2, 3)).apply(createPredicateContext(check))); +// assertTrue(filter(where("item").nin(asList((Object) null))).apply(createPredicateContext(check))); +// +// 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 From 7906aa30305d1be0c6f168e1806cd8e7dab5b1b6 Mon Sep 17 00:00:00 2001 From: Mythic Date: Wed, 28 Jun 2023 20:55:25 +0300 Subject: [PATCH 2/3] Revert commented tests and wildcard imports --- .../com/jayway/jsonpath/old/FilterTest.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java b/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java index 62b4f6b8..09de2f93 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java @@ -14,9 +14,11 @@ import java.util.regex.Pattern; import static com.jayway.jsonpath.Criteria.where; import static com.jayway.jsonpath.Filter.filter; import static java.util.Arrays.asList; -import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class FilterTest extends BaseTest { @@ -156,14 +158,14 @@ public class FilterTest extends BaseTest { check.put("item", 3); check.put("null_item", null); -// assertTrue(filter(where("item").nin(4, 5)).apply(createPredicateContext(check))); -// assertTrue(filter(where("item").nin(asList(4, 5))).apply(createPredicateContext(check))); -// assertTrue(filter(where("item").nin(asList('A'))).apply(createPredicateContext(check))); -// assertTrue(filter(where("null_item").nin(1, 2, 3)).apply(createPredicateContext(check))); -// assertTrue(filter(where("item").nin(asList((Object) null))).apply(createPredicateContext(check))); -// -// assertFalse(filter(where("item").nin(3)).apply(createPredicateContext(check))); -// assertFalse(filter(where("item").nin(asList(3))).apply(createPredicateContext(check))); + assertTrue(filter(where("item").nin(4, 5)).apply(createPredicateContext(check))); + assertTrue(filter(where("item").nin(asList(4, 5))).apply(createPredicateContext(check))); + assertTrue(filter(where("item").nin(asList('A'))).apply(createPredicateContext(check))); + assertTrue(filter(where("null_item").nin(1, 2, 3)).apply(createPredicateContext(check))); + assertTrue(filter(where("item").nin(asList((Object) null))).apply(createPredicateContext(check))); + + 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))); } From 770cd29c722209ca1abfe49aefebe026b20bfb23 Mon Sep 17 00:00:00 2001 From: Mythic Date: Wed, 28 Jun 2023 20:56:42 +0300 Subject: [PATCH 3/3] revert more wildcards --- .../test/java/com/jayway/jsonpath/old/FilterTest.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java b/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java index 09de2f93..716faa30 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java @@ -1,6 +1,11 @@ package com.jayway.jsonpath.old; -import com.jayway.jsonpath.*; +import com.jayway.jsonpath.BaseTest; +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; import org.junit.Test; @@ -14,11 +19,9 @@ import java.util.regex.Pattern; import static com.jayway.jsonpath.Criteria.where; import static com.jayway.jsonpath.Filter.filter; import static java.util.Arrays.asList; +import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; public class FilterTest extends BaseTest {