-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Regression: ClassCastException with contentConverter and Optional #4413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thank you for reporting this: it sounds like a bug indeed and would be good to fix before 2.17.0-final. I was trying to see if there was something in 2.17.0-rc1 release notes, as this sounds vaguely familiar report, but could not see anything immediately relevant looking. |
Hmmh. Java 8 modules does have something that looks potentially relevant: FasterXML/jackson-modules-java8#294 although fix itself would have been here in EDIT: local issue is #4337, and fix via PR #4338. Looks related enough... |
Looking at changes in #4338, I wonder if call to
might be the problem... although it appears to work for tested case. (note: while this serializer for |
Hmmh. Oh. Unfortunately, I'll take it back: I'm afraid things might be working as they actually should be -- and whatever 2.16 did was not the way things should work. Problem being that the definitions you have are incompatible:
since given for type Depending on how you want things to work, you could add |
Aaah, right. That makes a lot of sense now that you point it out. And yes, the proposed workaround works for me: diff a/Jackson2_17_Test.java b/Jackson2_17_Test.java
--- a/Jackson2_17_Test.java
+++ b/Jackson2_17_Test.java
@@ -1,4 +1,5 @@
import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
@@ -80,6 +81,7 @@ public class Jackson2_17_Test {
this.x5c = x5c;
}
+ @JsonIgnore
public Optional<List<X509Certificate>> getX5c() {
return Optional.ofNullable(x5c);
} Though take note of this from the documentation of
This is not a problem in my use case because my actual type has a I considered proposing a small update to the You may consider this resolved, I will fix the API misuse downstream. Thanks! |
`@JsonSerialize(contentConverter = ...)` is incompatible with property type `Optional<List<T>>`, which is the detected serialization type because of the auto-detected getter, because `contentConverter` only descends one container level. See: FasterXML/jackson-databind#4413 (comment)
@emlun Great! In the perfect world, Jackson could have figured this out but things do get quite complicated with multiple levels of nesting and possibly asymmetric types for accesors. So I am happy that you can make things work with the work-around. Improvements to (Java)Docs are always very welcome, FWTW -- so would appreciate any! |
Search before asking
Describe the bug
Using
@JsonSerialize(contentConverter = ...)
on a field with anOptional<...>
getter fails with Jackson2.17.0-rc1
but not with2.16.1
, with a runtime error like this:It appears the serializer attempts to match the input type of the content converter against the type of the collection itself instead of its contents.
Version Information
2.17.0-rc1
Reproduction
Full reproduction test case:
The tests
testWithoutContentConverter
andtestWithoutOptional
are expected to succeed andtestWithContentConverter
is expected to fail with Jackson2.17.0-rc1
. All three tests are expected to succeed with Jackson2.16.1
.Expected behavior
@JsonSerialize(contentConverter = ...)
should continue to work as it did in version2.16.1
.Additional context
Originally detected here: Yubico/java-webauthn-server#350
The text was updated successfully, but these errors were encountered: