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

Bring fork up-to-date with upstream changes. #3

Merged
merged 18 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Handle empty value objects properly (danielgtaylor#481)
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
  • Loading branch information
2 people authored and bbonenfant committed Jan 23, 2024
commit d87e4b45c9da2ecbe4ed012344910f5c3dbff4a6
7 changes: 7 additions & 0 deletions src/betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,13 @@ def __getattribute__(self, name: str) -> Any:
return value

def __setattr__(self, attr: str, value: Any) -> None:
if (
isinstance(value, Message)
and hasattr(value, "_betterproto")
and not value._betterproto.meta_by_field_name
):
value._serialized_on_wire = True

if attr != "_serialized_on_wire":
# Track when a field has been set.
self.__dict__["_serialized_on_wire"] = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ syntax = "proto3";

package google_impl_behavior_equivalence;

message Foo{
int64 bar = 1;
}
message Foo { int64 bar = 1; }

message Test{
oneof group{
message Test {
oneof group {
string string = 1;
int64 integer = 2;
Foo foo = 3;
}
}

message Request { Empty foo = 1; }

message Empty {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

import betterproto
from tests.output_betterproto.google_impl_behavior_equivalence import (
Empty,
Foo,
Request,
Test,
)
from tests.output_reference.google_impl_behavior_equivalence.google_impl_behavior_equivalence_pb2 import (
Empty as ReferenceEmpty,
Foo as ReferenceFoo,
Request as ReferenceRequest,
Test as ReferenceTest,
)


def test_oneof_serializes_similar_to_google_oneof():

tests = [
(Test(string="abc"), ReferenceTest(string="abc")),
(Test(integer=2), ReferenceTest(integer=2)),
Expand All @@ -30,7 +33,6 @@ def test_oneof_serializes_similar_to_google_oneof():


def test_bytes_are_the_same_for_oneof():

message = Test(string="")
message_reference = ReferenceTest(string="")

Expand All @@ -53,3 +55,16 @@ def test_bytes_are_the_same_for_oneof():

assert isinstance(message_reference.foo, ReferenceFoo)
assert isinstance(message_reference2.foo, ReferenceFoo)


def test_empty_message_field():
message = Request()
reference_message = ReferenceRequest()

message.foo = Empty()
reference_message.foo.CopyFrom(ReferenceEmpty())

assert betterproto.serialized_on_wire(message.foo)
assert reference_message.HasField("foo")

assert bytes(message) == reference_message.SerializeToString()