From fd48f6b249376c615ea237a3eabfc1ad53120a35 Mon Sep 17 00:00:00 2001 From: Vu Manh Dat Date: Sun, 26 Mar 2023 10:39:43 +0700 Subject: [PATCH 1/3] suppress exception when evaluate a json path. the Option.SUPRESS-EXCEPTION is not working in this case : var node = objectMapper.readTree("{\"test\" : null}"); JsonPath.using(Configuration.builder() .options(Option.SUPPRESS_EXCEPTIONS).build()) .parse(node.toString()) .read("$.test[?(@ != null)]"); --- .../java/com/jayway/jsonpath/JsonPath.java | 64 ++++++++++--------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index 01c7db03..2d7d3df5 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -168,42 +168,48 @@ public class JsonPath { boolean optAsPathList = configuration.containsOption(AS_PATH_LIST); boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST); boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); - - if (path.isFunctionPath()) { - if (optAsPathList || optAlwaysReturnList) { - if (optSuppressExceptions) { + try { + if (path.isFunctionPath()) { + if (optAsPathList || optAlwaysReturnList) { + if (optSuppressExceptions) { + return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray()); + } + throw new JsonPathException("Options " + AS_PATH_LIST + " and " + ALWAYS_RETURN_LIST + " are not allowed when using path functions!"); + } + EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); + if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray()); } - throw new JsonPathException("Options " + AS_PATH_LIST + " and " + ALWAYS_RETURN_LIST + " are not allowed when using path functions!"); - } - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); - if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { - return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray()); - } - return evaluationContext.getValue(true); - } else if (optAsPathList) { - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); - if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { - return (T) configuration.jsonProvider().createArray(); - } - return (T) evaluationContext.getPath(); - } else { - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); - if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { - if (optAlwaysReturnList) { + return evaluationContext.getValue(true); + } else if (optAsPathList) { + EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); + if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { return (T) configuration.jsonProvider().createArray(); + } + return (T) evaluationContext.getPath(); + } else { + EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); + if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { + if (optAlwaysReturnList) { + return (T) configuration.jsonProvider().createArray(); + } else { + return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray()); + } + } + Object res = evaluationContext.getValue(false); + if (optAlwaysReturnList && path.isDefinite()) { + Object array = configuration.jsonProvider().createArray(); + configuration.jsonProvider().setArrayIndex(array, 0, res); + return (T) array; } else { - return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray()); + return (T) res; } } - Object res = evaluationContext.getValue(false); - if (optAlwaysReturnList && path.isDefinite()) { - Object array = configuration.jsonProvider().createArray(); - configuration.jsonProvider().setArrayIndex(array, 0, res); - return (T) array; - } else { - return (T) res; + } catch (JsonPathException ex) { + if (optSuppressExceptions) { + return (T) configuration.jsonProvider().createArray(); } + throw ex; } } From 50688b852d1c80281fb7d8f6bfeec06380e8ae75 Mon Sep 17 00:00:00 2001 From: Dat Vu Date: Wed, 29 Mar 2023 10:46:06 +0700 Subject: [PATCH 2/3] update unit test --- .../test/java/com/jayway/jsonpath/Issue_908 | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/Issue_908 diff --git a/json-path/src/test/java/com/jayway/jsonpath/Issue_908 b/json-path/src/test/java/com/jayway/jsonpath/Issue_908 new file mode 100644 index 00000000..e905ca80 --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/Issue_908 @@ -0,0 +1,25 @@ +package com.jayway.jsonpath; + +import org.junit.Test; + +public class Issue_908 { + + @Test(expected = InvalidPathException.class) + public void test_when_current_context_is_null() { + JsonPath + .parse("{\"test\" : null }") + .read("$.test[?(@ != null)]"); + } + + @Test + public void test_suppress_exception_when_current_context_is_null() { + + Object rs = JsonPath.using(Configuration.builder() + .options(Option.SUPPRESS_EXCEPTIONS).build()) + .parse("{\"test\" : null }") + .read("$.test[?(@ != null)]"); + System.out.println(rs); + assert(rs.toString().equals("[]")); + } + +} From c258e6a5c506b55afab9c7dd73023c3969dc010c Mon Sep 17 00:00:00 2001 From: Dat Vu Date: Wed, 29 Mar 2023 10:47:23 +0700 Subject: [PATCH 3/3] Update and rename Issue_908 to Issue_908.java --- .../test/java/com/jayway/jsonpath/{Issue_908 => Issue_908.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename json-path/src/test/java/com/jayway/jsonpath/{Issue_908 => Issue_908.java} (95%) diff --git a/json-path/src/test/java/com/jayway/jsonpath/Issue_908 b/json-path/src/test/java/com/jayway/jsonpath/Issue_908.java similarity index 95% rename from json-path/src/test/java/com/jayway/jsonpath/Issue_908 rename to json-path/src/test/java/com/jayway/jsonpath/Issue_908.java index e905ca80..113b76bb 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/Issue_908 +++ b/json-path/src/test/java/com/jayway/jsonpath/Issue_908.java @@ -18,7 +18,7 @@ public class Issue_908 { .options(Option.SUPPRESS_EXCEPTIONS).build()) .parse("{\"test\" : null }") .read("$.test[?(@ != null)]"); - System.out.println(rs); + assert(rs.toString().equals("[]")); }