-
-
Notifications
You must be signed in to change notification settings - Fork 108
Support a flag to enable always emitting the discriminator field #1148
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
Support a flag to enable always emitting the discriminator field #1148
Conversation
This commit adds the `alwaysEmitDiscriminator` flag to CodecMakerConfig in order to support always emitting the discriminator field for ADT leaf classes. This new flag has no effect on generated decoders, and it does not effect encoder generation for classes that are not members of ADTs. This new flag also should have no effect on codecs derived for the root of the ADT.
@plokhotnyuk I noticed that this is currently failing
I noticed that previously there were some filters enabled to ignore those warnings, but it looks like they have been removed -- let me know if you'd like me to approach this another way to better preserve binary compatibility or if I am going about that wrong. |
This is a great feature given many API consumers will bind to a schema that always includes a type discriminator, no matter the context. Simply the fact that the type is part of a co-product hierarchy likely suggests that client libraries will expect it. We have experienced this issue. One could argue that this should even be the default, but obviously going that far would break existing users. |
@jamie-heller Thanks for your contribution! To pass MiMa checks need to increase a version number to |
Ah thanks! Just did in 6065e9c |
If I'm reading this documentation correctly (related conversation HERE) I guess this does indeed introduce a backwards compatibility issue -- which explains why the @plokhotnyuk Let me know which of the following approaches you'd like me to take to resolve this and I'll be happy to make the changes.
|
@jamie-heller Thanks! I've selected 3rd option because changes in the private primary constructor cannot break backward compatibility. I'll cut a release as soon as possible, but now I'm experiencing some hanging in the promote stage on oss.sonatype.org. |
Thanks @plokhotnyuk! I appreciate your responsiveness, and all your work on this library 🙂 |
Summary
This PR adds the
alwaysEmitDiscriminator
flag to CodecMakerConfig in order to support always emitting the discriminator field for ADT leaf classes. This new flag has no effect on generated decoders, and it does not effect encoder generation for classes that are not members of ADTs. This new flag also should have no effect on codecs derived for the root of the ADT.Motivation
The motivation for this PR is the use case of transitioning an existing API from a different json library to jsoniter-scala, wherein previously the type discriminators of ADT leaf classes were always serialized by the prior json library and the clients have come to depend upon it.
As a simplified example, consider the following exemplary data model:
The default behavior of jsoniter derived codecs is to only serialize the type discriminators when they are strictly required -- that is if during derivation a value is statically known to be a concrete ADT Leaf type, the derived codecs neither serialize the type discriminator nor require it. Thus by default, the following json is generated for instances of
Alpha
,Beta
, andGamma
respectivelyNote in this case the discriminator must be, and is, serialized
This PR enables setting a new flag in the CodecMakerConfig, namely
CodecMakerConfig.withAlwaysEmitDiscriminator(true)
, which changes the behavior to always emit the discriminator regardless of whether or not it is strictly required based upon the statically known type. Concretly, the 3 examples above would now become serialized like the following.Reading compatibility
Also note that while the serialized output above would be generated, the original json without the not-strictly-necessary discriminators would still successfully deserialize as expected.