Description
after i upgrade from 2.5 to 2.9, call through method JsonPath#delete
throws exception when delete a obj whitch not exist,
exception:
java.lang.ClassCastException: class com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to class java.util.List (com.fasterxml.jackson.databind.node.ArrayNode is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')
at com.jayway.jsonpath.internal.JsonContext.delete(JsonContext.java:159)
at com.jayway.jsonpath.internal.JsonContext.delete(JsonContext.java:154)
reason:
i used JacksonJsonNodeJsonProvider
as json provider.
when delete a obj not exist, with option Option.SUPPRESS_EXCEPTIONS
, it returns a ArrayNode
here:
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();
} else {
if (optAlwaysReturnList) {
return (T) configuration.jsonProvider().createArray();
} else {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
}
}
then it returns to JsonContext#delete
, and cause cast exception:
public DocumentContext delete(JsonPath path) {
List<String> modified = path.delete(json, configuration.addOptions(Option.AS_PATH_LIST));
// ...
}
i think it can be resolved by adjust generic capture in JsonContext#delete
, attention it not always return java.util.List