From b97f4cc17284df89baaaa33feaa1147d9031c9b9 Mon Sep 17 00:00:00 2001 From: Binokkio Date: Mon, 31 Oct 2022 22:50:32 +0100 Subject: [PATCH 1/2] Prevent ClassCastException when deleting missing path with suppress exceptions enabled --- .../java/com/jayway/jsonpath/JsonPath.java | 3 +- .../jsonpath/DeleteMissingPathTest.java | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/DeleteMissingPathTest.java 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..8d253156 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.Collections; import static com.jayway.jsonpath.Option.ALWAYS_RETURN_LIST; import static com.jayway.jsonpath.Option.AS_PATH_LIST; @@ -749,7 +750,7 @@ private T handleMissingPathInContext(final Configuration configuration) { boolean optAsPathList = configuration.containsOption(AS_PATH_LIST); boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST); if (optAsPathList) { - return (T) configuration.jsonProvider().createArray(); + return (T) Collections.emptyList(); } else { if (optAlwaysReturnList) { return (T) configuration.jsonProvider().createArray(); diff --git a/json-path/src/test/java/com/jayway/jsonpath/DeleteMissingPathTest.java b/json-path/src/test/java/com/jayway/jsonpath/DeleteMissingPathTest.java new file mode 100644 index 00000000..2bd18957 --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/DeleteMissingPathTest.java @@ -0,0 +1,65 @@ +package com.jayway.jsonpath; + +import com.jayway.jsonpath.spi.json.*; +import org.junit.Test; + +public class DeleteMissingPathTest { + + private DocumentContext getDocumentContextFromProvider(JsonProvider jsonProvider) { + + Configuration configuration = Configuration.builder() + .jsonProvider(jsonProvider) + .options(Option.SUPPRESS_EXCEPTIONS) + .build(); + + return JsonPath.parse("{}", configuration); + } + + @Test + public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_gson() { + getDocumentContextFromProvider(new GsonJsonProvider()) + .delete("$..this..path..is..missing"); + } + + @Test + public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_jackson_json_node() { + getDocumentContextFromProvider(new JacksonJsonNodeJsonProvider()) + .delete("$..this..path..is..missing"); + } + + @Test + public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_jackson() { + getDocumentContextFromProvider(new JacksonJsonProvider()) + .delete("$..this..path..is..missing"); + } + + @Test + public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_jakarta() { + getDocumentContextFromProvider(new JakartaJsonProvider()) + .delete("$..this..path..is..missing"); + } + + @Test + public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_jettison() { + getDocumentContextFromProvider(new JettisonProvider()) + .delete("$..this..path..is..missing"); + } + + @Test + public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_json_org() { + getDocumentContextFromProvider(new JsonOrgJsonProvider()) + .delete("$..this..path..is..missing"); + } + + @Test + public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_json_smart() { + getDocumentContextFromProvider(new JsonSmartJsonProvider()) + .delete("$..this..path..is..missing"); + } + + @Test + public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_tapestry() { + getDocumentContextFromProvider(new TapestryJsonProvider()) + .delete("$..this..path..is..missing"); + } +} From 31b448418d6b2fd2afd7cb7a3abd8be6dcee3859 Mon Sep 17 00:00:00 2001 From: Binokkio Date: Sat, 30 Nov 2024 08:30:59 +0100 Subject: [PATCH 2/2] Align with switch to junit-jupiter --- .../test/java/com/jayway/jsonpath/DeleteMissingPathTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-path/src/test/java/com/jayway/jsonpath/DeleteMissingPathTest.java b/json-path/src/test/java/com/jayway/jsonpath/DeleteMissingPathTest.java index 2bd18957..f204d7c6 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/DeleteMissingPathTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/DeleteMissingPathTest.java @@ -1,7 +1,7 @@ package com.jayway.jsonpath; import com.jayway.jsonpath.spi.json.*; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class DeleteMissingPathTest {