FasterXML / jackson-dataformats-binary

Uber-project for standard Jackson binary format backends: avro, cbor, ion, protobuf, smile
Apache License 2.0
314 stars 136 forks source link

[Avro] Including subtype as field #194

Open marcospassos opened 4 years ago

marcospassos commented 4 years ago

We have a use case where we want to add the subtype (logical name) mapped through JsonSubtypes as an virtual property, exactly as it does for Json. Currently, the annotation seems to be ignored.

Is there something we can do on our side to resolve the subtype name and include it on serialization and ignore on deserialization?

cowtowncoder commented 4 years ago

It should all "just work" if (and only if) matching Avro Schema has compatible definition, so I am not 100% sure what the problem is. Maybe a simple test case would help to show the issue?

marcospassos commented 4 years ago

@cowtowncoder yeah, we're mapping a string field named type, but the field does not get serialized. I believe that it's the expected behavior as Avro can resolve the subtype by using only the union information.

I think the "problem" here is that AvroModule overrides the AnnotationIntrospector and also the TypeIdResolver using the existing property strategy.

In our case, we use both using Json and Avro annotations so that we can serialize to both formats. However, a union of 15 types without a discriminating property is hard to interpret, so we'd like to include a property to discriminate against the subtype.

Do you see how we could overcome this limitation? I've spent most of my day experimenting with @JsonAppend and custom serialization without success.

As a serialization ninja, you probably should have an ace up your sleeve :)

cowtowncoder commented 4 years ago

Ahhhh. So the contributions for adding that were somewhat... Opinionated. I forgot that as it's not code I wrote or designed.

But one thing that could be done easily, I think, would be to add configurability to AvroAnntoationIntrospector. If you can find simple ways to do that, I could review changes.