Skip to content

Commit 8b5dd6c

Browse files
authored
Dont set group for optional fields (#528)
Fixes #523
1 parent 3514991 commit 8b5dd6c

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/betterproto/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ def dump(self, stream: BinaryIO) -> None:
909909
# Note that proto3 field presence/optional fields are put in a
910910
# synthetic single-item oneof by protoc, which helps us ensure we
911911
# send the value even if the value is the default zero value.
912-
selected_in_group = bool(meta.group)
912+
selected_in_group = bool(meta.group) or meta.optional
913913

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

src/betterproto/plugin/models.py

Lines changed: 4 additions & 1 deletion
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)