Inconsistency with google protobuf when converting to dict/json with include_default_values=True
Summary
When converting a message to dict (or to json) with include_default_values set to True, the dict/json message will contain all variants for a oneof, not just the set one.
This is inconsistent with the google protobuf library, and it can IMHO be confusing especially when variants are complex messages (as all the nested fields of all the variants will be contained in the dict/json)
Reproduction Steps
I created a simple repo to reproduce this, with a slightly more complete example, here.
But basically, given a message with a oneof field eg.
syntax = "proto3";
message Message {
oneof root_field {
int32 int_value = 1;
string string_value = 2;
}
}
Convert it to dict setting the including_default_values flag to True
from betterproto_compiled import Message
from pprint import pprint
message = Message(
int_value=10
)
pprint(message.to_dict(include_default_values=True))
Expected Results
I expected to obtain a dict with only the active variant of the oneof being included (which is the same behavior as the MessageToDict from the standard python protobuf library).
This is the output I get from google.protobuf.json_format.MessageToDict, setting including_default_value_fields=True:
{'intValue': 10}
Actual Results
A dict with all the variants of the oneof, including the non-set ones:
{'intValue': 10, 'stringValue': ''}
System Information
libprotoc 3.21.6
Python 3.10.13
Name: betterproto
Version: 2.0.0b6
Summary: A better Protobuf / gRPC generator & library
Home-page: https://github.com/danielgtaylor/python-betterproto
Author: Daniel G. Taylor
Author-email: [email protected]
License: MIT
Location: /home/marco/.local/share/rtx/installs/python/3.10.13/lib/python3.10/site-packages
Requires: grpclib, python-dateutil
Required-by:
Checklist
- [X] I have searched the issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have verified this issue occurs on the latest prelease of betterproto which can be installed using
pip install -U --pre betterproto, if possible.
+1 here.
The toy example given in the bug report fails to convey how noisy this can be in practice. I expect only the "in use" oneof to print, not all of them.