You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This should be the correct behavior for proto3, which is the version supported in betterproto. When you decode the message from the empty byte, the field should be set to the right value.
See the following example with Google's implementation (proto-plus):
When you decode the message from the empty byte, the field should be set to the right value.
How do you know whether the empty byte is null or false? If I understand correctly, having no modifier is the same as having the optional modifier, so b'' should be decoded as null but is, in fact, decoded as false.
If the field is not marked as optional, it is not possible for the field to have a null value. If you decode the message from the empty byte, you will see that the field will be actually set to False.
If the field is marked as optional, b'' will make the field have a null value.
Note that this behavior is not valid for messages, which can always have a null value.
Summary
When you don't add an explicit
optional
, fields are not serialized properly if they happen to be set to the default value.Reproduction Steps
generates
but the python code generated is
which implies that the boolean is non-nullable.
Then, if we do
it will be serialized to
and actually treated as null.
If we change the proto to
then the correct code is generated and the message is serialized as expected.
Actual Results
The protobuf documentation suggests that not setting
optional
should be treated in the same way as setting it.System Information
libprotoc 28.3
Python 3.10.11
betterproto 2.0.0b7
Checklist
pip install -U --pre betterproto
, if possible.The text was updated successfully, but these errors were encountered: