From 8b5dd6c1f8d299bfa82d9ac0b2c1107d8383c60c Mon Sep 17 00:00:00 2001 From: Erik Friese <64597681+124C41p@users.noreply.github.com> Date: Mon, 16 Oct 2023 04:43:42 +0200 Subject: [PATCH] Dont set `group` for optional fields (#528) Fixes #523 --- src/betterproto/__init__.py | 2 +- src/betterproto/plugin/models.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index da6e57fa8..bfda1c0bb 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -909,7 +909,7 @@ def dump(self, stream: BinaryIO) -> None: # Note that proto3 field presence/optional fields are put in a # synthetic single-item oneof by protoc, which helps us ensure we # send the value even if the value is the default zero value. - selected_in_group = bool(meta.group) + selected_in_group = bool(meta.group) or meta.optional # Empty messages can still be sent on the wire if they were # set (or received empty). diff --git a/src/betterproto/plugin/models.py b/src/betterproto/plugin/models.py index ea819d44d..1e22d8b3a 100644 --- a/src/betterproto/plugin/models.py +++ b/src/betterproto/plugin/models.py @@ -385,7 +385,10 @@ def is_oneof(proto_field_obj: FieldDescriptorProto) -> bool: us to tell whether it was set, via the which_one_of interface. """ - return which_one_of(proto_field_obj, "oneof_index")[0] == "oneof_index" + return ( + not proto_field_obj.proto3_optional + and which_one_of(proto_field_obj, "oneof_index")[0] == "oneof_index" + ) @dataclass