Skip to content

Commit bfc0fac

Browse files
authored
Enforce serialize_empty for repeated fields (danielgtaylor#417)
1 parent 8fbf447 commit bfc0fac

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

src/betterproto/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ def __bytes__(self) -> bytes:
806806
meta.proto_type,
807807
item,
808808
wraps=meta.wraps or "",
809+
serialize_empty=True,
809810
)
810811
# if it's an empty message it still needs to be represented
811812
# as an item in the repeated list
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto3";
2+
3+
package regression_387;
4+
5+
message Test {
6+
uint64 id = 1;
7+
}
8+
9+
message ParentElement {
10+
string name = 1;
11+
repeated Test elems = 2;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from tests.output_betterproto.regression_387 import (
2+
ParentElement,
3+
Test,
4+
)
5+
6+
7+
def test_regression_387():
8+
el = ParentElement(name="test", elems=[Test(id=0), Test(id=42)])
9+
binary = bytes(el)
10+
decoded = ParentElement().parse(binary)
11+
assert decoded == el
12+
assert decoded.elems == [Test(id=0), Test(id=42)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto3";
2+
3+
package regression_414;
4+
5+
message Test {
6+
bytes body = 1;
7+
bytes auth = 2;
8+
repeated bytes signatures = 3;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from tests.output_betterproto.regression_414 import Test
2+
3+
4+
def test_full_cycle():
5+
body = bytes([0, 1])
6+
auth = bytes([2, 3])
7+
sig = [b""]
8+
9+
obj = Test(body=body, auth=auth, signatures=sig)
10+
11+
decoded = Test().parse(bytes(obj))
12+
assert decoded == obj
13+
assert decoded.body == body
14+
assert decoded.auth == auth
15+
assert decoded.signatures == sig

0 commit comments

Comments
 (0)