Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit optional is not respected #645

Open
3 tasks done
hrist0stoichev opened this issue Nov 14, 2024 · 3 comments
Open
3 tasks done

Implicit optional is not respected #645

hrist0stoichev opened this issue Nov 14, 2024 · 3 comments
Labels
bug Something isn't working investigation needed

Comments

@hrist0stoichev
Copy link

hrist0stoichev commented Nov 14, 2024

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

message Sample {
    bool foo = 1
}

generates

[
 {
  "name": "foo",
  "type": "BOOLEAN",
  "mode": "NULLABLE",
 }
]

but the python code generated is

@dataclass(eq=False, repr=False)
class Sample(betterproto.Message):
    foo: bool = betterproto.string_field(1)

which implies that the boolean is non-nullable.

Then, if we do

bytes(Sample(foo=False))

it will be serialized to

b''

and actually treated as null.

If we change the proto to

message Sample {
    optional bool foo = 1
}

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

  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.
@hrist0stoichev hrist0stoichev added bug Something isn't working investigation needed labels Nov 14, 2024
@AdrienVannson
Copy link
Contributor

AdrienVannson commented Nov 25, 2024

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):

import proto

class Msg(proto.Message):
    field = proto.Field(proto.BOOL, number=1)

print(Msg.serialize(Msg(field=True)))  # b'\x08\x01'
print(Msg.serialize(Msg(field=False)))  # b''

@hrist0stoichev
Copy link
Author

hrist0stoichev commented Nov 25, 2024

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.

@AdrienVannson
Copy link
Contributor

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.

You can see this page for more details: https://protobuf.dev/programming-guides/field_presence/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working investigation needed
Projects
None yet
Development

No branches or pull requests

2 participants