Skip to content

Commit 061bf86

Browse files
committed
Set serialized_on_wire when message contains only lists
This fixes a bug where serialized_on_wire was not set when a message contained only repeated values (eg in a list or map). The fix here is to just set it to true in the `parse` method as soon as we receive any valid data. This also adds a test to expose the behavior.
1 parent eec24e4 commit 061bf86

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

betterproto/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,9 @@ def parse(self: T, data: bytes) -> T:
747747
self._unknown_fields += parsed.raw
748748
continue
749749

750+
# Got some data over the wire
751+
self._serialized_on_wire = True
752+
750753
meta = self._betterproto.meta_by_field_name[field_name]
751754

752755
value: Any
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
syntax = "proto3";
22

3+
package repeated;
4+
35
message Test {
46
repeated string names = 1;
57
}
8+
9+
service ExampleService {
10+
rpc DoThing (Test) returns (Test);
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Dict
2+
3+
import grpclib.const
4+
import grpclib.server
5+
import pytest
6+
from grpclib.testing import ChannelFor
7+
8+
import betterproto
9+
from betterproto.tests.output_betterproto.repeated.repeated import (
10+
ExampleServiceStub,
11+
Test,
12+
)
13+
14+
15+
class ExampleService:
16+
async def DoThing(
17+
self, stream: "grpclib.server.Stream[Test, Test]"
18+
):
19+
request = await stream.recv_message()
20+
await stream.send_message(request)
21+
22+
def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
23+
return {
24+
"/repeated.ExampleService/DoThing": grpclib.const.Handler(
25+
self.DoThing,
26+
grpclib.const.Cardinality.UNARY_UNARY,
27+
Test,
28+
Test,
29+
),
30+
}
31+
32+
33+
@pytest.mark.asyncio
34+
async def test_sets_serialized_on_wire() -> None:
35+
async with ChannelFor([ExampleService()]) as channel:
36+
stub = ExampleServiceStub(channel)
37+
response = await stub.do_thing(names=['a', 'b', 'c'])
38+
assert betterproto.serialized_on_wire(response)
39+
assert response.names == ['a', 'b', 'c']

0 commit comments

Comments
 (0)