Skip to content

Commit

Permalink
Prevent ClassCastException when deleting missing path with suppress e…
Browse files Browse the repository at this point in the history
…xceptions enabled
  • Loading branch information
binokkio committed Oct 31, 2022
1 parent 9729cb0 commit eaa5e9b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -749,7 +750,7 @@ private <T> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit eaa5e9b

Please sign in to comment.