Summary
There should be an alternative bytes method that allows to serialize zeroes for proto2 syntax. Currently zeroes enums are skipped as they are "default" values which is incorrect because unless the field is optional, it must be present in encoded form.
Reproduction Steps
- Create a simple proto2 file:
message MyMessage {
enum MyEnum {
FOOBAR = 0;
BAZ = 1;
}
}
- Compile to python:
@dataclass
class MyMessage(betterproto.Message):
# @required
action: "MyEnum" = betterproto.enum_field(1)
class MyEnum(betterproto.Enum):
FOOBAR = 0
BAZ = 1
- Use the
FOOBAR value (0):
print(
MyMessage(
action=MyEnum.FOOBAR,
)
)
You can see the MyEnum.FOOBAR here. However when you try to serialize it, the encoder skips this value incorrectly thinking it's default and hence shouldn't be added:
print(
MyMessage(
action=MyEnum.FOOBAR,
).to_json()
)
# or
print(
MyMessage(
action=MyEnum.FOOBAR,
).to_dict()
)
# or
print(
bytes(MyMessage(
action=MyEnum.FOOBAR,
))
)
This can only be "fixed" by passing include_default_values=True in to_json or to_dict:
print(
MyMessage(
action=MyEnum.FOOBAR,
).to_json(include_default_values=True)
)
But I need binary representation of the MyMessage class
Expected Results
N/A
Actual Results
N/A
System Information
Latest beta version
Checklist
Summary
There should be an alternative bytes method that allows to serialize zeroes for proto2 syntax. Currently zeroes enums are skipped as they are "default" values which is incorrect because unless the field is optional, it must be present in encoded form.
Reproduction Steps
FOOBARvalue (0):You can see the MyEnum.FOOBAR here. However when you try to serialize it, the encoder skips this value incorrectly thinking it's default and hence shouldn't be added:
This can only be "fixed" by passing include_default_values=True in to_json or to_dict:
But I need binary representation of the MyMessage class
Expected Results
N/A
Actual Results
N/A
System Information
Latest beta version
Checklist
pip install -U --pre betterproto, if possible.