0
votes

I have a grpc client written in GO and a grpc server written in Java (both using the same proto files (syntax 2).

My grpc method takes a message that may contain extensions. I am able to construct a message containing desired extensions on the client and send it to the server. But when I try to read the message on the server, my extensions are available as unknown fields. (In other words, entity.hasExtension(extension) in Java returns false).

So my question is whether grpc allows extensions to be used in messages that are provided as method parameters. If not, is there a way to convert an unknown field to a field of specific type?

My proto file:

syntax = "proto2";

// proto file used as source for go client and java server as well

package my_services;
import "basic_types.proto";
// import "extension_types.proto";

// do not delete: options for generating java code
option java_multiple_files = true;
option java_package = "myservice.grpc";
option java_outer_classname = "MyServiceWrapper";
option objc_class_prefix = "Foo";

// Interface exposed by the server.
service DataService {
  // Obtains all objects satisfying the request message
  rpc MyMethod(DataRequest) returns (DataResponse) {}
}

message DataRequest {
  optional  IdDefinition                        id = 1;
  repeated  basic_types.Entity           templates = 2;
}

message DataResponse {
  repeated IdDefinition                         id = 1;
  optional basic_types.DataResult           result = 2;
}

message IdDefinition {
  optional  int32 myid = 1;
}

basic_types.Entity is a basic message containing extensions:

message Entity {
    extensions 1 to max;
}

and may be extended e.g. like this:

extend basic_types.Entity {
  optional Foo    foo = 1000;
  optional Bar    bar = 1001;    
}

Any help or hint would be much appreciated.

1

1 Answers

0
votes

In java it is possible, but you need to set an extension registry with ProtoLiteUtils.setExtensionRegistry(). This is an experimental API, and there may be a different way in the future to do this, but for the time being it should be useable.

More generally, All message encodings are supported by gRPC. We natively support Proto3, but there are a lot of existing Proto2 users that use gRPC. Since gRPC is encoding agnostic, you can even use things like thrift or JSON if you really want to, though we don't automatically generate stubs for those.