You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot read Optionals written with StdTypeResolverBuilder #86
changed the title [-]Cannot read read written Optionals with StdTypeResolverBuilder [/-][+]Cannot read Optionals written with StdTypeResolverBuilder [/+]on Oct 12, 2018
I will have to say that it is quite possible that combination of polymorphic type in Optional for root value may be unsupportable. I would strongly recommend not using that combination: partly since root values have significant challenges wrt type erasure, and partly as Optionals are challenging in their own right.
In fact I am considering adding a warning that polymorphic type handling is not recommended to be used for root values at all.
Having said that: I will try to figure out if this can be made to work correctly (although depending on changes they might only be made to 2.9 branch), and I hope this is the case.
Thank you for reporting the problem.
EDIT: Looking at this further, Optional is NOT used as root value. So that's not the problem.
If we try to read above JSON output from 2.8, in later version, throws error saying...
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'java.util.Optional' as a subtype of `java.lang.Integer`: Not a subtype
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 4, column: 13] (through reference chain: com.fasterxml.jackson.datatype.jdk8.OptionalStdTypeResolverBuilder86Test$Foo["bar"])
at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
at com.fasterxml.jackson.databind.DeserializationContext.invalidTypeIdException(DeserializationContext.java:2041)
at com.fasterxml.jackson.databind.DatabindContext._throwNotASubtype(DatabindContext.java:291)
at com.fasterxml.jackson.databind.DatabindContext.resolveAndValidateSubType(DatabindContext.java:252)
at com.fasterxml.jackson.databind.jsontype.impl.ClassNameIdResolver._typeFromId(ClassNameIdResolver.java:76)
Hmmmh. Now that I think about this, there's a problem... Although we do need the type id of the contained value, we also need to know there's Optional in-between.
So not 100% sure what type id is needed. It's almost as if we needed Optional<Integer>.
To further complicate things tho, Integer is so-called "natural" type, for which no type id is to be output. Ugh.
I also realized that the test case is incomplete; it doesn't seem to verify actual contents.
Activity
[-]Cannot read read written Optionals with StdTypeResolverBuilder [/-][+]Cannot read Optionals written with StdTypeResolverBuilder [/+]isopov commentedon Oct 24, 2018
Added workaround, involving copy&pasting classes from 2.8.11 - https://github.com/isopov/jackson-optional-test/blob/workaround/src/test/java/com/example/OptionalJacksonTest.java
Still proper fix will be highly appreciated.
cowtowncoder commentedon Oct 30, 2018
I will have to say that it is quite possible that combination of polymorphic type in
Optionalfor root value may be unsupportable. I would strongly recommend not using that combination: partly since root values have significant challenges wrt type erasure, and partly asOptionals are challenging in their own right.In fact I am considering adding a warning that polymorphic type handling is not recommended to be used for root values at all.
Having said that: I will try to figure out if this can be made to work correctly (although depending on changes they might only be made to 2.9 branch), and I hope this is the case.
Thank you for reporting the problem.
EDIT: Looking at this further,
Optionalis NOT used as root value. So that's not the problem.cowtowncoder commentedon Aug 19, 2019
To help future work, output looks like:
and that would seem to be missing type information for contents, somehow.
cowtowncoder commentedon Dec 7, 2024
@JooHyukKim this might be worth looking into as well.
JooHyukKim commentedon Dec 7, 2024
Ah so this is how we want the output object is in 2.8.
{ "com.fasterxml.jackson.datatype.jdk8.OptionalStdTypeResolverBuilder86Test$Foo": { "bar": { "java.util.Optional": 1 } } }If we try to read above JSON output from 2.8, in later version, throws error saying...
cowtowncoder commentedon Dec 7, 2024
Hmmmh. Now that I think about this, there's a problem... Although we do need the type id of the contained value, we also need to know there's
Optionalin-between.So not 100% sure what type id is needed. It's almost as if we needed
Optional<Integer>.To further complicate things tho,
Integeris so-called "natural" type, for which no type id is to be output. Ugh.I also realized that the test case is incomplete; it doesn't seem to verify actual contents.