Skip to content

Commit

Permalink
Fix serialization of repeated fields with empty messages
Browse files Browse the repository at this point in the history
Extend test config and utils to support exclusion of certain json samples from
testing for symetry.
  • Loading branch information
nat-n committed Apr 5, 2021
1 parent 7c5ee47 commit 31c63b3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ def encode_varint(value: int) -> bytes:

def _preprocess_single(proto_type: str, wraps: str, value: Any) -> bytes:
"""Adjusts values before serialization."""

if proto_type in [
TYPE_ENUM,
TYPE_BOOL,
Expand Down Expand Up @@ -738,9 +739,18 @@ def __bytes__(self) -> bytes:
output += _serialize_single(meta.number, TYPE_BYTES, buf)
else:
for item in value:
output += _serialize_single(
meta.number, meta.proto_type, item, wraps=meta.wraps or ""
output += (
_serialize_single(
meta.number,
meta.proto_type,
item,
wraps=meta.wraps or "",
)
# if it's an empty message it still needs to be represented
# as an item in the repeated list
or b"\n\x00"
)

elif isinstance(value, dict):
for k, v in value.items():
assert meta.map_types
Expand Down
7 changes: 7 additions & 0 deletions tests/inputs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@
"example_service",
"empty_service",
}


# Indicate json sample messages to skip when testing that json (de)serialization
# is symmetrical becuase some cases legitimately are not symmetrical.
# For tuple in the set, the first value is the name of the test and the second is
# the name of the json file.
non_symmetrical_json = {("empty_repeated", "empty_repeated")}
3 changes: 3 additions & 0 deletions tests/inputs/empty_repeated/empty_repeated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"msg": [{"values":[]}]
}
9 changes: 9 additions & 0 deletions tests/inputs/empty_repeated/empty_repeated.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

message MessageA {
repeated float values = 1;
}

message Test {
repeated MessageA msg = 1;
}

0 comments on commit 31c63b3

Please sign in to comment.