Skip to content

Editions2024 adds custom option symbols to generated code in Java #22918

@beatrausch

Description

@beatrausch

Hi,

I was looking for a way to ignore symbols in generated code for custom options. I stumbled over the announcement of editions 2024 that introduces import option which might do what I am looking for.
In java it seems not to work.

Alternatives considered

  • With protoc in combination with syntax2, syntax3 and editions2023 I was not able to get generated code without symbols for custom options.
  • Removing options and generate code multiple times, with and without options.

What did you do?
Assume the following proto files:

// editions/apis/domain/echo/echo.proto
edition = "2024";

package domain.echo;

option csharp_namespace = "Domain.Echo";
option java_package = "org.domain.echo";

import option "sample/api/annotations.proto";


message EchoRequest {
  string some_value = 1 [
    (sample.api.field_option) = {
      value: "hallo"
    }
  ];
}

//  thrid-party/sample/api/annotations.proto
syntax = "proto2";

package sample.api;

import "google/protobuf/descriptor.proto";

option go_package = "sample.org/api;api";

message FieldOption {
  required string value = 1;
}

extend google.protobuf.FieldOptions {
  optional FieldOption field_option = 5000;
}

Commands

mkdir -p output/editions/csharp
mkdir -p output/editions/java
mkdir -p output/editions/cpp

protoc -I. -Iproto/include -Ithrid-party -Ieditions/apis  --option_dependencies=sample/api/annotations.proto \
		--cpp_out=output/editions/cpp \
		--csharp_out=output/editions/csharp \
		--java_out=output/editions/java \
		editions/apis/domain/echo/echo.proto

What did you expect to see
No referenced symbol/import/include related to the annotations.proto

What did you see instead?
Generated code adds fieldOption to registry registry.add(sample.api.Annotations.fieldOption);

Generated java code of EchoProto.java

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// NO CHECKED-IN PROTOBUF GENCODE
// source: editions/apis/domain/echo/echo.proto
// Protobuf Java Version: 4.32.0-rc1

package org.domain.echo;

@com.google.protobuf.Generated
public final class EchoProto {
  private EchoProto() {}
  static {
    com.google.protobuf.RuntimeVersion.validateProtobufGencodeVersion(
      com.google.protobuf.RuntimeVersion.RuntimeDomain.PUBLIC,
      /* major= */ 4,
      /* minor= */ 32,
      /* patch= */ 0,
      /* suffix= */ "-rc1",
      EchoProto.class.getName());
  }
  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistryLite registry) {
  }

  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistry registry) {
    registerAllExtensions(
        (com.google.protobuf.ExtensionRegistryLite) registry);
  }
  static final com.google.protobuf.Descriptors.Descriptor
    internal_static_domain_echo_EchoRequest_descriptor;
  static final 
    com.google.protobuf.GeneratedMessage.FieldAccessorTable
      internal_static_domain_echo_EchoRequest_fieldAccessorTable;

  public static com.google.protobuf.Descriptors.FileDescriptor
      getDescriptor() {
    return descriptor;
  }
  private static  com.google.protobuf.Descriptors.FileDescriptor
      descriptor;
  static {
    java.lang.String[] descriptorData = {
      "\n$editions/apis/domain/echo/echo.proto\022\013" +
      "domain.echo\032\030domain/types/types.proto\"X\n" +
      "\013EchoRequest\022\037\n\nsome_value\030\001 \001(\tB\013\302\270\002\007\n\005" +
      "hallo\022(\n\006common\030\002 \001(\0132\030.domain.types.Com" +
      "monTypeB5\n\017org.domain.echoZ\024domain.org/e" +
      "cho;echo\252\002\013Domain.Echob\010editionsp\351\007z\034sam" +
      "ple/api/annotations.proto"
    };
    descriptor = com.google.protobuf.Descriptors.FileDescriptor
      .internalBuildGeneratedFileFrom(descriptorData,
        new com.google.protobuf.Descriptors.FileDescriptor[] {
          org.domain.types.TypesProto.getDescriptor(),
        });
    internal_static_domain_echo_EchoRequest_descriptor =
      getDescriptor().getMessageTypes().get(0);
    internal_static_domain_echo_EchoRequest_fieldAccessorTable = new
      com.google.protobuf.GeneratedMessage.FieldAccessorTable(
        internal_static_domain_echo_EchoRequest_descriptor,
        new java.lang.String[] { "SomeValue", "Common", });
    descriptor.resolveAllFeaturesImmutable();
    org.domain.types.TypesProto.getDescriptor();
    com.google.protobuf.ExtensionRegistry registry =
        com.google.protobuf.ExtensionRegistry.newInstance();
    registry.add(sample.api.Annotations.fieldOption);
    com.google.protobuf.Descriptors.FileDescriptor
        .internalUpdateFileDescriptor(descriptor, registry);
  }

  // @@protoc_insertion_point(outer_class_scope)
}

What version of protobuf and what language are you using?
protoc --version
libprotoc 32.0-rc1
Language: C++/Java/ C#

What operating system (Linux, Windows, ...) and version?
Linux 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions