fullstorydev / grpcurl

Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers
MIT License
10.36k stars 497 forks source link

string interpreted as StringValue #381

Closed eum2o closed 1 year ago

eum2o commented 1 year ago

Tested with v1.8.7:

Apparently grpcurl auto-boxes string into a google.protobuf.StringValues.

Example

Take this protobuf:

syntax = "proto3";

import "google/protobuf/wrappers.proto";

message Foobar {
  string s1 = 1;
  google.protobuf.StringValue s2 = 2;
}

I can successfully use grpcurl by calling something like

grpcurl -d '{"s1": "val1", "s2": "val2"}' ...

which is weird because I'd expect the JSON to look as follows:

grpcurl -d '{"s1": "val1", "s2": { "value" : "val2" }}' ...

But I'f use the latter, I get the error

Error invoking method "<foobar>": error getting request data: json: cannot unmarshal object into Go value of type string
jhump commented 1 year ago

@eum2o, this is expected behavior. grpcurl uses the canonical JSON format for protobuf: https://protobuf.dev/programming-guides/proto3/#json

In that table, you'll see a row for "Wrapper types", one of which is google.protobuf.StringValue. You'll see in that table that all of the wrapper types are encoded in JSON as just the value they wrap -- not as a JSON object.

eum2o commented 1 year ago

@jhump I wasn't aware. Thank you for the nice explanation.