JacksonJsonNodeJsonProvider throws class cast exceptions
ahaverdings-deloitte opened this issue · 11 comments
This JacksonJsonNodeJsonProvider throws class cast exceptions:
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')
Resulting that the body is never transformed when there is one rule applied that handels arrays. Once the exception is throw the original body is returned (and this line also hides the exception pretty well...)
Issue is that the expected type of (T) would be List but JacksonJsonNodeJsonProvider on createArray() is returning an ArrayNode which is not castable to List.
A solution would be to use JacksonJsonProvider instead of JacksonJsonNodeJsonProvider
Where is this cast to a List happening?
The cast itself is implicit and part of JsonPath
(and maybe not much to do about it):
https://github.com/json-path/JsonPath/blob/3f4900d6e7361cd21f0327c828dabf83fd188227/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java#L131
then:
https://github.com/json-path/JsonPath/blob/3f4900d6e7361cd21f0327c828dabf83fd188227/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java#L225
then:
https://github.com/json-path/JsonPath/blob/3f4900d6e7361cd21f0327c828dabf83fd188227/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java#L752
the line it starts is here:
Sounds like a bug in json-path then. Maybe there was a change in Jackson which caused this incompatibility?
I got the same error in the logs. The class cast exception does also occurs when trying to delete a node that does not exists.
The following test shows an error:
void test(){
BodyFilter filter = JsonPathBodyFilters.jsonPath("$.invalid").delete();
System.out.println(filter.filter("application/json","{"key_1":"value 1","key_2":"value 2"}"));
}
I have run into this issue as well. There is a bug report on the json-path project json-path/JsonPath#678 that looks to be related.
I've tried copying the JsonPathBodyFilters to a new class and changing the implementation to the JacksonJsonProvider
as @ahaverdings-deloitte mentions above.
This does resolve the issue of the ClassCastException
but I'm not 100% sure what the impact of making that change is
In the meantime, as a workaround I've used the JsonBodyFilters
instead of the path:
JsonBodyFilters.replaceJsonStringProperty(Set.of("fieldName"), CONTENT_REPLACEMENT_TEXT);
Also this log can be supressed by adding this to your logback.xml for now:
<!-- quiet down the logging for logbook json filters -->
<logger name="org.zalando.logbook.json.JsonPathBodyFilters" level="INFO"/>
In order to prioritize the support for Logbook, we would like to check whether the old issues are still relevant.
This issue has not been updated for over six months.
- Please check if it is still relevant in latest version of the Logbook.
- If so, please add a descriptive comment to keep the issue open.
- Otherwise, the issue will automatically be closed after a week.