3

The first proto file(main.proto) in offline directory

option java_package = "com.xxx.proto";
option java_outer_classname = "Service1";
option java_multiple_files = true;

message Response {
    repeated Entity entity = 1;
}

message Entity {}

and here I have the second proto file(recent.proto) which is in recent

option java_package = "com.xxx.proto";
option java_outer_classname = "Service2";
option java_multiple_files = true;

message Response {
    repeated Entity entity = 1;
    repeated Entity.Type type =2;
}
message Entity {}

And I receive protoc: stdout: . stderr: offline/main.proto: "offline.Response.entitiy" is already defined in file "recent/recent.proto". Want to mention Entity are different for both cases, the class name is the same

I.S
  • 1,904
  • 2
  • 25
  • 48
  • Isn't it obvious? it is trying to generate classes under the same package with the same name. It doesn't really matter that the parameters are different. – Stf Kolev Jul 15 '20 at 13:44
  • It is not the same name , java_outer_classname it is different – I.S Jul 15 '20 at 15:54

1 Answers1

1

Assuming the protobuf package is the same(which in this current case is), then the messages have the same protobuf name. So it is definitely a problem.

The maven plugin may have built each file as a separate invocation to protoc, which wouldn't have noticed. The gradle plugin uses a single invocation of protoc, which has advantages and isn't something I expect to change (but others may feel differently).

If you generated these are two separate projects, then it would probably be fine. Otherwise, protobuf needs to be able to refer to the messages. That is done by name. The name is proto package + message name, potentially with nesting.

The appropriate way to do what you want is to use nested messages (or simply use different names):

message Request {
  message Foo {}
}
Stf Kolev
  • 640
  • 1
  • 8
  • 21