Protobuf RPC Service method without parameters
Asked Answered
O

2

99

I'm trying to describe an RPC service using Google's Protocol Buffers

service WhoamiService {
  rpc WhoAreYou() returns (Whoami) {}
}

message Whoami {
  optional bytes request_id = 1;
  optional string hostname = 2;
  optional string message = 3;
}

When I try to compile this definition, I get an error Expected type name pointing to the WhoAreYou() piece.

It works fine if I replace WhoAreYou() with WhoAreYou(Whoami), but in this case, the method does not need any parameters.. Is there a way to do this or is it simply not supported?

Ogata answered 16/4, 2015 at 22:44 Comment(1)
visitors to this page might also be interested in https://mcmap.net/q/117256/-can-i-define-a-grpc-call-with-a-null-request-or-response and google.protobuf.EmptyJohore
B
147

You have to specify an input type. If you don't want the method to take any parameters, define an empty message type, like:

message WhoAreYouParams {}

The reason this is required is so that if you later need to add an optional parameter, you can do so without breaking existing code.

Boniface answered 17/4, 2015 at 18:14 Comment(1)
I'm wondering, by "breaking existing code", you mean breaking at a source level, right? Compiled clients will simply ignore messages which suddenly have new fields in them.Howler
F
145

You can specify google.protobuf.Empty instead of your own empty message. Example:

rpc WhoAreYou(google.protobuf.Empty) returns (Whoami) {
}

Don't forget to import appropriate proto file:

import "google/protobuf/empty.proto";
Faina answered 19/7, 2018 at 15:9 Comment(4)
This is the more precise answer.Thoraco
It's not recommended to do because there is no way to change it later with backward compatibility.Pirandello
"It's not recommended to do because there is no way to change it later with backward compatibility." –– I would assume that changing the request or response message from Empty to something non-empty wouldn't break existing users of this service. So we're only talking about breaking at source level, where suddenly you need to modify source code of clients using the service. Is that right? (I cannot find any documentation on evolving gRPC services)Howler
** As well not supported in 2.6 verMetacarpal

© 2022 - 2024 — McMap. All rights reserved.