Skip to content

Commit 342e655

Browse files
Properly serialize zero-value messages in a oneof group (#176)
Also improve test utils to make it easier to have multiple json examples. Co-authored-by: Christopher Chambers <[email protected]>
1 parent 2f62189 commit 342e655

File tree

13 files changed

+101
-44
lines changed

13 files changed

+101
-44
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ generate = { script = "tests.generate:main", help = "Generate test cases (do
4949
test = { cmd = "pytest --cov src", help = "Run tests" }
5050
types = { cmd = "mypy src --ignore-missing-imports", help = "Check types with mypy" }
5151
format = { cmd = "black . --exclude tests/output_", help = "Apply black formatting to source code" }
52-
clean = { cmd = "rm -rf .coverage .mypy_cache .pytest_cache dist betterproto.egg-info **/__pycache__ tests/output_*", help = "Clean out generated files from the workspace" }
5352
docs = { cmd = "sphinx-build docs docs/build", help = "Build the sphinx docs"}
5453
bench = { shell = "asv run master^! && asv run HEAD^! && asv compare master HEAD", help = "Benchmark current commit vs. master branch"}
54+
clean = { cmd = "rm -rf .asv .coverage .mypy_cache .pytest_cache dist betterproto.egg-info **/__pycache__ tests/output_*", help = "Clean out generated files from the workspace" }
5555

5656
# CI tasks
5757
full-test = { shell = "poe generate && tox", help = "Run tests with multiple pythons" }

src/betterproto/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def __bytes__(self) -> bytes:
699699
meta.number,
700700
meta.proto_type,
701701
value,
702-
serialize_empty=serialize_empty,
702+
serialize_empty=serialize_empty or selected_in_group,
703703
wraps=meta.wraps or "",
704704
)
705705

tests/inputs/oneof/test_oneof.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
def test_which_count():
77
message = Test()
8-
message.from_json(get_test_case_json_data("oneof"))
8+
message.from_json(get_test_case_json_data("oneof")[0])
99
assert betterproto.which_one_of(message, "foo") == ("count", 100)
1010

1111

1212
def test_which_name():
1313
message = Test()
14-
message.from_json(get_test_case_json_data("oneof", "oneof-name.json"))
14+
message.from_json(get_test_case_json_data("oneof", "oneof_name.json")[0])
1515
assert betterproto.which_one_of(message, "foo") == ("name", "foobar")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"nothing": {}
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
syntax = "proto3";
2+
3+
message Nothing {}
4+
5+
message MaybeNothing {
6+
string sometimes = 42;
7+
}
8+
9+
message Test {
10+
oneof empty {
11+
Nothing nothing = 1;
12+
MaybeNothing maybe1 = 2;
13+
MaybeNothing maybe2 = 3;
14+
}
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"maybe1": {}
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"maybe2": {
3+
"sometimes": "now"
4+
}
5+
}

tests/inputs/oneof_empty/test_oneof_empty.py

Whitespace-only changes.

tests/inputs/oneof_enum/test_oneof_enum.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ def test_which_one_of_returns_enum_with_default_value():
1414
returns first field when it is enum and set with default value
1515
"""
1616
message = Test()
17-
message.from_json(get_test_case_json_data("oneof_enum", "oneof_enum-enum-0.json"))
17+
message.from_json(
18+
get_test_case_json_data("oneof_enum", "oneof_enum-enum-0.json")[0]
19+
)
1820

1921
assert message.move == Move(
2022
x=0, y=0
@@ -28,7 +30,9 @@ def test_which_one_of_returns_enum_with_non_default_value():
2830
returns first field when it is enum and set with non default value
2931
"""
3032
message = Test()
31-
message.from_json(get_test_case_json_data("oneof_enum", "oneof_enum-enum-1.json"))
33+
message.from_json(
34+
get_test_case_json_data("oneof_enum", "oneof_enum-enum-1.json")[0]
35+
)
3236
assert message.move == Move(
3337
x=0, y=0
3438
) # Proto3 will default this as there is no null
@@ -38,7 +42,7 @@ def test_which_one_of_returns_enum_with_non_default_value():
3842

3943
def test_which_one_of_returns_second_field_when_set():
4044
message = Test()
41-
message.from_json(get_test_case_json_data("oneof_enum"))
45+
message.from_json(get_test_case_json_data("oneof_enum")[0])
4246
assert message.move == Move(x=2, y=3)
4347
assert message.signal == Signal.PASS
4448
assert betterproto.which_one_of(message, "action") == ("move", Move(x=2, y=3))

0 commit comments

Comments
 (0)