1

For any field x of type string, protoc seems to generate two java accessors:

  • getX() that returns a String
  • getXBytes() that returns a ByteString

While the getXBytes() method may be useful, I am surprised that this behavior doesn't seem to documented in the official protoc Java documentation.

Moreover, this introduces a potential name conflict issue. Take for example the following proto definition:

syntax = "proto3";

message Message {
  string x = 2;
  string x_bytes = 3;
}

Here is the corresponding interface generated by protoc

  public interface MessageOrBuilder extends
      // @@protoc_insertion_point(interface_extends:Message)
      com.google.protobuf.MessageOrBuilder {

    /**
     * <code>string x = 2;</code>
     * @return The x.
     */
    java.lang.String getX();
    /**
     * <code>string x = 2;</code>
     * @return The bytes for x.
     */
    com.google.protobuf.ByteString
        getXBytes();

    /**
     * <code>string x_bytes = 3;</code>
     * @return The xBytes.
     */
    java.lang.String getXBytes();
    /**
     * <code>string x_bytes = 3;</code>
     * @return The bytes for xBytes.
     */
    com.google.protobuf.ByteString
        getXBytesBytes();
  }
  /**

This is invalid java because two methods are named getXBytes.

Interestingly, protoc does seem to handle similar name collisions for repeated fields, as described in the documentation: "If another non-repeated field has a name that conflicts with one of the repeated field’s generated methods, both field names will have their protobuf field number appended to the end.".

This seems like an inconsistency (or a bug). Did I miss something here? Is there a recommended way to handle such name conflicts?

Any insights or clarification would be greatly appreciated!

4
  • "Is there a recommended way to handle such name conflicts?" Don't write fields with those names. Commented Apr 29 at 16:54
  • @Remy, Protoc generates getXBytes() for every string field, and it does not consistently resolve naming conflicts like it does for repeated fields. Your conflict between x and x_bytes causes invalid Java code. To avoid this, rename fields to prevent overlaps (e.g., avoid using _bytes suffix for strings). Commented Apr 29 at 19:09
  • Thank you for your suggestion! Unfortunately, this proto definition is part of another project that I do not own. As the file is modified frequently, I will have to add an ad-hoc tweaking process in my CI to rename the conflicting fields. Ideally, I should convince the author of the conflicting proto to rename those fields (he doesn’t encounter this problem because he does not use Java). However, this may be a bit challenging since the generation of the getXBytes() method is not clearly documented. Commented Apr 29 at 19:54
  • It's a little strange to have two fields where x and x_bytes are useful names. Commented May 1 at 1:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.