grpc-ecosystem / grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec
https://grpc-ecosystem.github.io/grpc-gateway/
BSD 3-Clause "New" or "Revised" License
18.19k stars 2.24k forks source link

Can you differentiate between an empty map vs field not provided? #552

Closed Ianwww closed 6 years ago

Ianwww commented 6 years ago

With this proto definition

 message UpdateMessage {
    string id = 1;
    map[string]string my_map = 2;
 }

 service YourService {
  rpc Echo(UpdateMessage) returns (StringMessage) {
    option (google.api.http) = {
      patch: "/update"
      body: "*"
    };
  }
 }

I'd like to send a request like {"id":"myid","my_map":{}} and set MyMap to an empty map.

Is this possible? Currently, when this field is provided, my service thinks this field is unset(MyMap==Nil). I'm working in golang.

achew22 commented 6 years ago

I, unfortunately, am not as expert with protos as I probably should be, but I don't believe there is a easy way to do that that doesn't involve fieldmasks which we don't currently support. That said there is some interest in implementing that in the gateway. Would you be interested in taking that sort of project on?

Ianwww commented 6 years ago

thanks for the response. I had to move forward and use a wrapper around a map to handle this. I'm not going to be able to work on this with my present workload.

johanbrandhorst commented 6 years ago

You have to use field masks. This will be automatically supported with #671.

mayank-dixit commented 5 years ago

With this proto definition

 message UpdateMessage {
    string id = 1;
    map[string]string my_map = 2;
 }

 service YourService {
  rpc Echo(UpdateMessage) returns (StringMessage) {
    option (google.api.http) = {
      patch: "/update"
      body: "*"
    };
  }
 }

I'd like to send a request like {"id":"myid","my_map":{}} and set MyMap to an empty map.

Is this possible? Currently, when this field is provided, my service thinks this field is unset(MyMap==Nil). I'm working in golang.

@ Ianwww @achew22 Use this maybe: https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/StringValue