Skip to content

Commit 8bcb67b

Browse files
Merge pull request #81 from discord/serialized_on_wire_repeated
Always set serialized_on_wire for all parsed message fields
2 parents 3273ae4 + 28a2889 commit 8bcb67b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

betterproto/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,9 @@ def parse(self: T, data: bytes) -> T:
724724
Parse the binary encoded Protobuf into this message instance. This
725725
returns the instance itself and is therefore assignable and chainable.
726726
"""
727+
# Got some data over the wire
728+
self._serialized_on_wire = True
729+
727730
for parsed in parse_fields(data):
728731
field_name = self._betterproto.field_name_by_number.get(parsed.number)
729732
if not field_name:

betterproto/tests/test_features.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import betterproto
22
from dataclasses import dataclass
3-
from typing import Optional
3+
from typing import Optional, List, Dict
44

55

66
def test_has_field():
@@ -32,6 +32,25 @@ class Foo(betterproto.Message):
3232
foo.bar = Bar()
3333
assert betterproto.serialized_on_wire(foo.bar) is False
3434

35+
@dataclass
36+
class WithCollections(betterproto.Message):
37+
test_list: List[str] = betterproto.string_field(1)
38+
test_map: Dict[str, str] = betterproto.map_field(
39+
2, betterproto.TYPE_STRING, betterproto.TYPE_STRING
40+
)
41+
42+
# Is always set from parse, even if all collections are empty
43+
with_collections_empty = WithCollections().parse(bytes(WithCollections()))
44+
assert betterproto.serialized_on_wire(with_collections_empty) == True
45+
with_collections_list = WithCollections().parse(
46+
bytes(WithCollections(test_list=["a", "b", "c"]))
47+
)
48+
assert betterproto.serialized_on_wire(with_collections_list) == True
49+
with_collections_map = WithCollections().parse(
50+
bytes(WithCollections(test_map={"a": "b", "c": "d"}))
51+
)
52+
assert betterproto.serialized_on_wire(with_collections_map) == True
53+
3554

3655
def test_class_init():
3756
@dataclass

0 commit comments

Comments
 (0)