Skip to content

JacksonJsonNodeJsonProvider makes ClassCastException while JsonPath#delete #1039

Open
@GAATTC0

Description

@GAATTC0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions