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

Set serialized_on_wire when message contains only lists #81

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
FuegoFro committed Jun 4, 2020
commit 061bf86a9cce960d0bdff78baabb7269c7facd3f
3 changes: 3 additions & 0 deletions betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,9 @@ def parse(self: T, data: bytes) -> T:
self._unknown_fields += parsed.raw
continue

# Got some data over the wire
self._serialized_on_wire = True

meta = self._betterproto.meta_by_field_name[field_name]

value: Any
Expand Down
6 changes: 6 additions & 0 deletions betterproto/tests/inputs/repeated/repeated.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
syntax = "proto3";

package repeated;

message Test {
repeated string names = 1;
}

service ExampleService {
rpc DoThing (Test) returns (Test);
}
39 changes: 39 additions & 0 deletions betterproto/tests/inputs/repeated/test_repeated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Dict

import grpclib.const
import grpclib.server
import pytest
from grpclib.testing import ChannelFor

import betterproto
from betterproto.tests.output_betterproto.repeated.repeated import (
ExampleServiceStub,
Test,
)


class ExampleService:
async def DoThing(
self, stream: "grpclib.server.Stream[Test, Test]"
):
request = await stream.recv_message()
await stream.send_message(request)

def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
return {
"/repeated.ExampleService/DoThing": grpclib.const.Handler(
self.DoThing,
grpclib.const.Cardinality.UNARY_UNARY,
Test,
Test,
),
}


@pytest.mark.asyncio
async def test_sets_serialized_on_wire() -> None:
async with ChannelFor([ExampleService()]) as channel:
stub = ExampleServiceStub(channel)
response = await stub.do_thing(names=['a', 'b', 'c'])
assert betterproto.serialized_on_wire(response)
assert response.names == ['a', 'b', 'c']