json-path / JsonPath

Java JsonPath implementation
Apache License 2.0
8.86k stars 1.64k forks source link

ClassCastException class com.google.gson.JsonArray cannot be cast to class java.util.List #787

Open OlehHoliuk opened 2 years ago

OlehHoliuk commented 2 years ago

I am using GsonJsonProvider and it returns JsonArray from createArray method. JsonArray is not implementing List interface, but Iterable. This body of delete method in JsonContext expects to receive List and not Iterable: image

Context: I am calling delete method on multiple json objects, and some of them may not contain nodes that I want to delete. I am using Option.SUPPRESS_EXCEPTIONS for this case. And when there is no such path in my object it is fallbacks to the recently added JsonPath#handleMissingPathInContext(configuration) method, which return configuration.jsonProvider().createArray() (the JsonArray) and delete method fails with java.lang.ClassCastException: class com.google.gson.JsonArray cannot be cast to class java.util.List

simmons-square commented 2 years ago

Seeing the same error after updating from 2.4.0 to 2.7.0 to resolve some vulnerabilities.

I pinpointed the issue to this method: https://github.com/json-path/JsonPath/blob/8cdaaef9009b0dd0d9335ffb1ccbe03b7fe8a586/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java#L748-L760

For GsonJsonProvider, the createArray method returns a JsonArray. https://github.com/json-path/JsonPath/blob/8cdaaef9009b0dd0d9335ffb1ccbe03b7fe8a586/json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java#L144

The caller (https://github.com/json-path/JsonPath/blob/8cdaaef9009b0dd0d9335ffb1ccbe03b7fe8a586/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java#L280) is generic and just returns the result.

The caller to that (https://github.com/json-path/JsonPath/blob/8cdaaef9009b0dd0d9335ffb1ccbe03b7fe8a586/json-path%2Fsrc%2Fmain%2Fjava%2Fcom%2Fjayway%2Fjsonpath%2Finternal%2FJsonContext.java#L159) expects a List<String>.