Skip to content

Commit 5a57226

Browse files
124C41pcetanu
authored andcommitted
Dont set group for optional fields (#528)
Fixes #523
1 parent af387d7 commit 5a57226

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/betterproto/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def dump(self, stream: BinaryIO) -> None:
917917
# Note that proto3 field presence/optional fields are put in a
918918
# synthetic single-item oneof by protoc, which helps us ensure we
919919
# send the value even if the value is the default zero value.
920-
selected_in_group = bool(meta.group)
920+
selected_in_group = bool(meta.group) or meta.optional
921921

922922
# Empty messages can still be sent on the wire if they were
923923
# set (or received empty).

src/betterproto/plugin/models.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,10 @@ def is_oneof(proto_field_obj: FieldDescriptorProto) -> bool:
385385
us to tell whether it was set, via the which_one_of interface.
386386
"""
387387

388-
return which_one_of(proto_field_obj, "oneof_index")[0] == "oneof_index"
388+
return (
389+
not proto_field_obj.proto3_optional
390+
and which_one_of(proto_field_obj, "oneof_index")[0] == "oneof_index"
391+
)
389392

390393

391394
@dataclass

0 commit comments

Comments
 (0)