Skip to content

Fix protobuf oneof default values#3147

Open
xiaozhikang0916 wants to merge 2 commits intoKotlin:devfrom
xiaozhikang0916:fix-protobuf-oneof-default-values
Open

Fix protobuf oneof default values#3147
xiaozhikang0916 wants to merge 2 commits intoKotlin:devfrom
xiaozhikang0916:fix-protobuf-oneof-default-values

Conversation

@xiaozhikang0916
Copy link
Copy Markdown
Contributor

Override shouldEncodeElementDefault in OneOfElementEncoder to always return true. This ensures oneof fields are encoded even when they contain default values.

According to protobuf oneof semantics, the selected case must always be identifiable. So a tag with field number is always needed for one of field.

Fixes #3144

And I found that code sample in the issue shows some misusing of default values in proto messages. So I add an extra section in docs for it.

Override `shouldEncodeElementDefault` in `OneOfElementEncoder` to always
return true. This ensures oneof fields are encoded even when they contain
default values (e.g., 0 for int, "" for string, or empty message).

According to protobuf oneof semantics, the selected case must always be
identifiable. Without this fix, encoding a oneof with default values
would produce an empty output, making it impossible to distinguish which
variant was selected during decoding.

Fixes Kotlin#3144
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant