Skip to content

Set serialized_on_wire when message contains only lists #81

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,9 @@ def parse(self: T, data: bytes) -> T:
Parse the binary encoded Protobuf into this message instance. This
returns the instance itself and is therefore assignable and chainable.
"""
# Got some data over the wire
self._serialized_on_wire = True

for parsed in parse_fields(data):
field_name = self._betterproto.field_name_by_number.get(parsed.number)
if not field_name:
Expand Down
21 changes: 20 additions & 1 deletion betterproto/tests/test_features.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import betterproto
from dataclasses import dataclass
from typing import Optional
from typing import Optional, List, Dict


def test_has_field():
Expand Down Expand Up @@ -32,6 +32,25 @@ class Foo(betterproto.Message):
foo.bar = Bar()
assert betterproto.serialized_on_wire(foo.bar) == False

@dataclass
class WithCollections(betterproto.Message):
test_list: List[str] = betterproto.string_field(1)
test_map: Dict[str, str] = betterproto.map_field(
2, betterproto.TYPE_STRING, betterproto.TYPE_STRING
)

# Is always set from parse, even if all collections are empty
with_collections_empty = WithCollections().parse(bytes(WithCollections()))
assert betterproto.serialized_on_wire(with_collections_empty) == True
with_collections_list = WithCollections().parse(
bytes(WithCollections(test_list=["a", "b", "c"]))
)
assert betterproto.serialized_on_wire(with_collections_list) == True
with_collections_map = WithCollections().parse(
bytes(WithCollections(test_map={"a": "b", "c": "d"}))
)
assert betterproto.serialized_on_wire(with_collections_map) == True


def test_class_init():
@dataclass
Expand Down