Closed pymaxion-amzn closed 3 years ago
Looks like this issue is related to (or maybe duplicates) an older, still-open issue: [Ion] Polymorphic deserialization does not work correctly #149
Hasn't this been resolved by PR #232 ?
Yes, this was resolved by PR #232. I'll close the issue now, thanks for the reminder!
I have not done too much investigation into this problem, but it appears that the
@JsonTypeInfo
annotation might not be handled properly by theIonObjectMapper
code path. I discovered this issue while trying to upgrade my Jackson dependencies from version 2.8.x to 2.10.x, which involved switching from an old, now-deprecatedJacksonIon
library onto this one.Let's say that I have the following classes, which approximately model my real-world use case:
I would expect the following test to succeed, but it sadly does not:
This test succeeds with the older
JacksonIon
library, but does not succeed now. Specifically, the test throws an exception when executingionValueMapper.parse(instanceAsIon, BaseClass.class)
, something like:Indeed, when looking at the serialized data, it does not match what I expect based on what I provided in the
@JsonTypeInfo
annotation:The exception message is accurate in that the serialized version does not have an
@class
type id property; the type id information is instead written as an Ion annotation.Note that although this example involves polymorphism, I wasn't able to get the test to succeed even with non-polymorphic types, for example:
I get the same exception as before when performing round-trip serialization and deserialization with
IonObjectMapper
.It seems as though the serialization logic is ignoring the
@JsonTypeInfo
annotation and always choosing to write the type info in an Ion annotation, while the deserialization logic is reading the annotation and therefore expects the type info in a property named@class
, and it throws an exception when no such property is found.Moreover, for reasons relating to backwards compatibility, I would really like to serialize an instance of
Subclass
with the type info in an@class
property rather than an Ion annotation, but after poking around in the source code, it seems the decision to write type ids as Ion annotations is more-or-less hard-coded:Just to unblock myself, I've been playing around with various workarounds, including extending
IonGenerator
and supplying it toIonObjectMapper
via my own customIonFactory
, as well as changing the behavior of the methods I linked above based on a new configuration option which can be set on theIonObjectMapper
. However, I'm not completely sure why@JsonTypeInfo
is being ignored during serialization in the first place, and don't know enough about the internals of Jackson at the moment to investigate effectively.This is potentially related to this old pull request: https://github.com/FasterXML/jackson-dataformats-binary/pull/109. I don't know how much overlap there might be between these two issues.