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

Enforce serialize_empty for repeated fields #417

Merged
merged 5 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions src/betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ def __bytes__(self) -> bytes:
meta.proto_type,
item,
wraps=meta.wraps or "",
serialize_empty=True,
)
# if it's an empty message it still needs to be represented
# as an item in the repeated list
Expand Down
12 changes: 12 additions & 0 deletions tests/inputs/regression_387/regression_387.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package regression_387;

message Test {
uint64 id = 1;
}

message ParentElement {
string name = 1;
repeated Test elems = 2;
}
12 changes: 12 additions & 0 deletions tests/inputs/regression_387/test_regression_387.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from tests.output_betterproto.regression_387 import (
Test,
ParentElement,
)


def test_regression_387():
el = ParentElement(name="test", elems=[Test(id=0), Test(id=42)])
binary = bytes(el)
decoded = ParentElement().parse(binary)
assert decoded == el
assert decoded.elems == [Test(id=0), Test(id=42)]
9 changes: 9 additions & 0 deletions tests/inputs/regression_414/regression_414.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package regression_414;

message Test {
bytes body = 1;
bytes auth = 2;
repeated bytes signatures = 3;
}
15 changes: 15 additions & 0 deletions tests/inputs/regression_414/test_regression_414.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from tests.output_betterproto.regression_414 import Test


def test_full_cycle():
body = bytes([0, 1])
auth = bytes([2, 3])
sig = [b""]

obj = Test(body=body, auth=auth, signatures=sig)

decoded = Test().parse(bytes(obj))
assert decoded == obj
assert decoded.body == body
assert decoded.auth == auth
assert decoded.signatures == sig