Open jobarr-amzn opened 3 years ago
Ok. Just to make sure I understand this correctly, the concept of native type ids is somewhat orthogonal to @JsonTypeInfo
: it was added after @JsonTypeInfo
existed. Or maybe better way is to say that @JsonTypeInfo
indicates logical concept of type id, and native/non-native distinction (for formats that have native type ids) is separate questions.
As such @JsonTypeInfo
has no role to play in decision: it has nothing to indicate such handling at this point.
Streaming API, on the other hand, exposes existence of native type ids. But due to existing design, it can only indicate it globally (use native for all or none), not for specific types.
But even if @JsonTypeInfo
(or some new possibly format-specific annotation) could indicate use (or not) of native mechanism on per-type basis, databind would not currently have any way to connect that in.
With that, the current choice really is that user needs to indicate usage on per-call (read, write) basis: default setting exists for ObjectMapper
(or more accurately, underlying TokenStreamFactory
, passing it to parser/generator being created), and can be changed on per-call basis.
Generator (subtype of JsonGenerator
) has bit more say on actual inclusion of type id, with writeTypeId()
(native) and writeTypePrefix()
/writeTypeSuffix()
(non-native) calls.
I am not sure if that helps in figuring out what to do with the change discussed here but thought it might help at least understand constraints.
One quick question/note here: I am hoping to get 2.12.1 released in about 1 week (on Dec 29 or 30), and I think that this issue itself is not a blocked since the default behavior would be the same as that of 2.12.0. Another thing to note is that since this module was only officially release as part of 2.9 (although backdated versions exist) so in a way behavior of 2.9.0 could be considered canonical. That does not really help anyone who started with an earlier version of course...
Anyway: I guess what I am asking is just that if there is something urgent wrt 2.12.1 itself (regarding #232 that added setting to allow avoiding writing of native type ids), please add a note here (or file separate issue for specific change; I think this is bigger question altogether).
is something urgent wrt 2.12.1 itself
With #232 we have a path forward. This question isn't urgent- I don't even know for sure that it has a satisfying answer. I don't know from Jackson's perspective what's feasible or advisable here so I'm floating this issue to raise a discussion.
Another thing to note is that since this module was only officially release as part of 2.9 (although backdated versions exist) so in a way behavior of 2.9.0 could be considered canonical.
Agreed 100%, I make the same argument myself. jackson-dataformat-ion
default behavior shouldn't change from the only released behavior it has ever had.
That does not really help anyone who started with an earlier version of course...
True :) If upgrading from the unreleased ("2.8") type serialization behavior to jackson-dataformat-ion
2.12 you have to make sure that all services pick up necessary code changes or we end up with mixed mode fleets during deployment etc. etc.
I don't understand how all the pieces fit together here yet- it doesn't seem to me that @JsonTypeInfo
is orthogonal to native type ids since use of the annotation can specify exactly how type information should be encoded and without the annotation no type information is encoded at all. It feels off that specifying "include type information as a property named '@class'" can result in a behavior different from that. I still don't get all the constraints though.
Sounds good.
As to Native Type Id vs @JsonTypeInfo
: I agree that conceptually they should be connected; what I tried to explain is how pieces as they are currently do not connect. Since @JsonTypeInfo
was added first, it has no awareness of possibility of native type id. There are other complications too, since Jackson annotations are mostly format-agnostic, including @JsonTypeInfo
; and native type id support can be conditional as well as format-specific.
But the most pertinent part I guess is that none of @JsonTypeInfo
metadata (properties it has, representation within databind) is related to possibility of native type id.
That is the situation at this point, anyway. I am open to improvement suggestions, as always.
As to path forward it sounds like this issue does not block 2.12.1, and that #232 should be included even if it is likely incomplete solution wrt problem of using (or not) of native type ids.
jackson-dataformat-ion polymorphic behaviors
SSCCE to demonstrate behaviors.
This sample code depends on
jackson-dataformat-ion
,jackson-annotations
,jackson-databind
, andjackson-core
.Available jackson-dataformat-ion behaviors:
v2.8
@JsonTypeInfo
annotations, no type information without explicit guidance.We need to modify the example to work with 2.8:
Afterwards it should yield:
Output
v2.9 - Present (2.12 release)
As.PROPERTY
from@JsonTypeInfo
annotation.@JsonTypeInfo
still needed to cause typed serialization.Assuming 2.10+ for consistent
IonValue
import statement.Output
After PR #232
Output
After PR #232, with native types disabled at Mapper construction
Output
Open Question: What is the ideal behavior?
After PR #232 both achievable
IonMapper
configurations won't serialize type data without e.g. a POJO annotation, but write behavior can seem a little surprising. A user who has attempted to configure property-based serialization of type information will be surprised to see it showing up as an annotation instead.An argument can be made that the most locally or specifically expressed user preference should control behavior, in which case a
@JsonTypeInfo
annotation should override format-native behavior. I.e. the serialization ofSubclass
above should naturally be:This is complicated by the fact that
As.Property
is the default for@JsonTypeInfo
. Take this example:The user has not specified
include
oras
and it's not obvious that the user does not want format-native type serialization behavior.This behavior doesn't emerge from
jackson-dataformat-ion
directly, but from JsonGenerator injackson-core
, AsPropertyTypeSerializer injackson-databind
, etc.I'm looking for some commentary on intent here, and what options might be available. It's not clear to me how much flexibility there is here.