Skip to content

Commit eaa5e9b

Browse files
committed
Prevent ClassCastException when deleting missing path with suppress exceptions enabled
1 parent 9729cb0 commit eaa5e9b

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.net.URL;
27+
import java.util.Collections;
2728

2829
import static com.jayway.jsonpath.Option.ALWAYS_RETURN_LIST;
2930
import static com.jayway.jsonpath.Option.AS_PATH_LIST;
@@ -749,7 +750,7 @@ private <T> T handleMissingPathInContext(final Configuration configuration) {
749750
boolean optAsPathList = configuration.containsOption(AS_PATH_LIST);
750751
boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
751752
if (optAsPathList) {
752-
return (T) configuration.jsonProvider().createArray();
753+
return (T) Collections.emptyList();
753754
} else {
754755
if (optAlwaysReturnList) {
755756
return (T) configuration.jsonProvider().createArray();
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.jayway.jsonpath;
2+
3+
import com.jayway.jsonpath.spi.json.*;
4+
import org.junit.Test;
5+
6+
public class DeleteMissingPathTest {
7+
8+
private DocumentContext getDocumentContextFromProvider(JsonProvider jsonProvider) {
9+
10+
Configuration configuration = Configuration.builder()
11+
.jsonProvider(jsonProvider)
12+
.options(Option.SUPPRESS_EXCEPTIONS)
13+
.build();
14+
15+
return JsonPath.parse("{}", configuration);
16+
}
17+
18+
@Test
19+
public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_gson() {
20+
getDocumentContextFromProvider(new GsonJsonProvider())
21+
.delete("$..this..path..is..missing");
22+
}
23+
24+
@Test
25+
public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_jackson_json_node() {
26+
getDocumentContextFromProvider(new JacksonJsonNodeJsonProvider())
27+
.delete("$..this..path..is..missing");
28+
}
29+
30+
@Test
31+
public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_jackson() {
32+
getDocumentContextFromProvider(new JacksonJsonProvider())
33+
.delete("$..this..path..is..missing");
34+
}
35+
36+
@Test
37+
public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_jakarta() {
38+
getDocumentContextFromProvider(new JakartaJsonProvider())
39+
.delete("$..this..path..is..missing");
40+
}
41+
42+
@Test
43+
public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_jettison() {
44+
getDocumentContextFromProvider(new JettisonProvider())
45+
.delete("$..this..path..is..missing");
46+
}
47+
48+
@Test
49+
public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_json_org() {
50+
getDocumentContextFromProvider(new JsonOrgJsonProvider())
51+
.delete("$..this..path..is..missing");
52+
}
53+
54+
@Test
55+
public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_json_smart() {
56+
getDocumentContextFromProvider(new JsonSmartJsonProvider())
57+
.delete("$..this..path..is..missing");
58+
}
59+
60+
@Test
61+
public void test_delete_missing_path_with_suppress_exceptions_does_not_throw_tapestry() {
62+
getDocumentContextFromProvider(new TapestryJsonProvider())
63+
.delete("$..this..path..is..missing");
64+
}
65+
}

0 commit comments

Comments
 (0)