proto3: Why setting java_multiple_files = true not creating separate java classes for each proto message?

121 views
Skip to first unread message

tuk

unread,
Apr 9, 2022, 7:49:51 AM4/9/22
to Protocol Buffers

Cross-posting from stackoverflow

I have a proto definition with nested classes

syntax = "proto3"; 
option java_package = "com.spot.proto.v1.config"; 
package v1config; 
 
message ClusterConfig { 
  enum LogLevel { 
      UNKNOWN_TYPE = 0; DEBUG = 1; INFO = 2; WARN = 3; ERROR = 4; FATAL = 5; 
 } 
  message Network { 
     string ip = 1; 
     int32 port = 2; 
  } 
  message Constants { 
       message SlackNotifier { 
             message Channel { 
                string name = 1; 
             } 
           string auth_token = 1; 
            map<string, Channel> channels = 2; 
            bool is_enabled = 3; 
   } 
   message Kafka { 
         string broker_list = 1; 
   } 
   Kafka kafka = 2; 
 
  message Support { 
     Network network = 1; 
 } 
 Support support = 1; 
 ... 
 }

When I generate the java classes with option java_multiple_files = false; it creates a single java class, ClusterConfigOuterClass.java with all the Java classes/enums/etc generated for the top-level messages, services, and enumerations nested inside it. This is expected.

But if I use option java_multiple_files = true; then I am seeing it is generating two additional classes ClusterConfig.java, ClusterConfigOrBuilder.java along with ClusterConfigOuterClass.javaClusteConfig.java now contains the nested classes.

The documentation states like below:-

java_multiple_files (file option): If false, only a single .java file will be generated for this .proto file, and all the Java classes/enums/etc. generated for the top-level messages, services, and enumerations will be nested inside of an outer class (see java_outer_classname). If true, separate .java files will be generated for each of the Java classes/enums/etc. generated for the top-level messages, services, and enumerations, and the wrapper Java class generated for this .proto file won't contain any nested classes/enums/etc. This is a Boolean option that defaults to false. If not generating Java code, this option has no effect.

So should not each nested message like Kafka, Network, etc go into a separate java file?

  • Java 11
  • Protoc - 3.10

Deanna Garcia

unread,
May 2, 2022, 2:22:37 PM5/2/22
to Protocol Buffers
The documentation that you copied says:

If true, separate .java files will be generated for each of the Java classes/enums/etc. generated for the top-level messages, services, and enumerations, and the wrapper Java class generated for this .proto file won't contain any nested classes/enums/etc`

Since nested messages aren't top-level, the generated code for nested messages will be located in it's top-level message file.

Reply all
Reply to author
Forward
0 new messages