Skip to content
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

[Protobuf] consider disabling encoding of null #2908

Open
shanshin opened this issue Jan 21, 2025 · 0 comments
Open

[Protobuf] consider disabling encoding of null #2908

shanshin opened this issue Jan 21, 2025 · 0 comments
Labels

Comments

@shanshin
Copy link
Contributor

What is your use-case and why do you need this feature?
Protobuf naturally does not support passing null value.
As a workaround, kotlinx serialization uses the approach when a nullable property contains null, it is not encoded.

However, this approach does not work well when the property is a collection or optional.

In this case, ambiguity appears during decoding if the field value is missing, because decoder cannot figures out which value should be decoded: an empty collection, default value, or null.

Describe the solution you'd like
We need to consider the option where null will always not be encoded. This approach may be better suited for integration with serialization libraries that do not support null in protobuf.
This mode can be enabled by default, or enabled with a separate flag.

In any of these cases, there should be an annotation that changes the behavior of one specific field on which it is specified: if the mode is enabled by default, the annotation disables it for this field, or vice versa.

Symmetry of decoding

In this mode, decoding will be asymmetric, so encoding optional nullable fields will become dangerous if encodeDefaults is not enabled.

We also need to think about collections, whether an empty collection and null are always the same thing when decoding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant