Grpc-gateway make strange wrapping of com.google.protobuf.wrappers.StringValue result

374 views Asked by At

I have GRPC service with the following function:

rpc StreamMessages(StreamMessagesRequest) returns (stream google.protobuf.StringValue) {
    option (google.api.http) = {
        post: "/messages:stream"body: "*"
    };
}

With grpc-gateway behind it.

Once I have collection of 3 strings: "msg1", "msg2", "msg3" - wrapping every one as com.google.protobuf.wrappers.StringValue and returning as stream. On GRPC side everything fine, but when I'm trying to execute REST request via gateway the issue happens:

According to documentation, Json representation of google.protobuf.StringValue is just JsonString, so expected streaming result is:

"msg1"
"msg2"
"msg3"

But it returns unexpected format instead:

{"result":"msg1"}
{"result":"msg2"}
{"result":"msg3"}

Question: How can I cause gateway to return the expected?

1

There are 1 answers

0
bogdanciobanu On

In order to obtain what you're looking for you need to use the protobuf specific package for the json marshaling. Instead of the standard one, use this one: google.golang.org/protobuf/encoding/protojson.

The interface is the same, but it correctly marshals StringValues to strings when an actual value is provided and the fields get ignored if the StringValue pointer is nil.