Protobuf: to use same Message name with different packages

Viewed 552

I am using protobuf java, with following .proto

// service1.proto
option java_package = "package";
option java_outer_classname = "Proto1";
message M {
   ... // some definition
}

and

// service2.proto
option java_package = "package";
option java_outer_classname = "Proto2";
message M {
   ... // some different definition
}

while compile, error is thrown in service2.proto saying that "M" is already defined in service1.proto

But from package and generated code they should be package.Proto1.M and package.Proto2.M, is this conflict?

1 Answers

The "package" is also a .proto concept (not just a language/framework concept); if you need to have both schemas involved in anything, it may be useful to add

package Proto1;

to service1.proto and

package Proto2;

to service2.proto


Alternatively, if the M is actually the same in both places: move M to a different single file, and use import from both service1.proto and service2.proto

Related