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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshal response from streaming #2189

Closed
pavolloffay opened this issue Jun 17, 2021 · 6 comments
Closed

Unmarshal response from streaming #2189

pavolloffay opened this issue Jun 17, 2021 · 6 comments
Labels

Comments

@pavolloffay
Copy link

馃殌 Feature

The streaming response is wrapped into result object (see #1254, applies for v1 and v2). This makes it impossible to unmarshal (via JSONPb) the response body into protoc generated objects (MyResponse) in this case. The response can be unmarshalled only if the "result" envelope is removed (e.g. string manipulation).

message MyResponse {
  string message = 1;
}

service QueryService {
  rpc Get(Request) returns (stream MyResponse) {}
}

The response body looks like:

{
  "result": {
    "message": "hello"
}

Is there a way we could improve this? I have tried to define a proto message (see below) that could be used to remove the envelope.

message ResultWrapper {
  bytes result = 1;
// google.protobuf.Any result = 1; // does not work either bc the JSON does not have type annotation https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto#L110
}
@johanbrandhorst
Copy link
Collaborator

An alternative way to unmarshal it is to wrap your response message type in the envelope type manually:

type Envelope struct {
    Result *yourpb.YourType `json:"result"`
    Error *statuspb.Status `json:"error"`
}

@pavolloffay
Copy link
Author

Yes I ended up doing something like this in our tests.

type resultWrapper struct {
	Result json.RawMessage `json:"result"`
}

I am not sure if using the proto type in the envelope would work as expected bc JSONPb should be used for unmarshalling.

@johanbrandhorst
Copy link
Collaborator

You can use jsonpb with a custom type too. Another alternative is to generate the openapiv2 spec and then generate a client using that, it should be able to parse the streaming messages too.

@stale
Copy link

stale bot commented Aug 21, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@Prabhjot-Sethi
Copy link

An alternative way to unmarshal it is to wrap your response message type in the envelope type manually:

type Envelope struct {
    Result *yourpb.YourType `json:"result"`
    Error *statuspb.Status `json:"error"`
}

Is there any documentation on how error reporting per entry works, can server control how and what error goes to client.
usually grpc only provides mechanism to report error while starting the stream

@johanbrandhorst
Copy link
Collaborator

gRPC generally supports returning an error at any point in the streaming, so the grpc-gateway must also be able to return an error at any point in the streaming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants