Handle empty value objects properly (#481)
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
This commit is contained in:
		| @@ -704,6 +704,13 @@ class Message(ABC): | ||||
|             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 | ||||
|   | ||||
| @@ -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 {} | ||||
| @@ -3,17 +3,20 @@ from google.protobuf import json_format | ||||
|  | ||||
| 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)), | ||||
| @@ -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="") | ||||
|  | ||||
| @@ -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() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user