Skip to content

[Protobuf] consider disabling encoding of null #2908

Open
@shanshin

Description

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.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions