protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Drop prefixes from Java enum values if there's a prefix which matches the name of the enum

Open fioan89 opened this issue 1 year ago • 9 comments

What language does this apply to? If it's a proto syntax change, is it for proto2 or proto3? If it's about generated code change, what programming language?

The feature request is for the Java language in combination with proto3

Describe the problem you are trying to solve.

Protobuf does not consider the name of the enum as part of the scope, only the package name. This is inconvenient for cases where two enum types have similar enum values. A typical approach is to prefix the enum values with the name of the enum type but this quite inconvenient for a couple of reasons:

  • it does not preserve the idioms and conventions of the Java language.
  • it affects readability and maintainability of the code
  • promotes repetitiveness

Describe the solution you'd like

The generated Java code should have the enum values without the prefix. In java, enums are considered classes, and hence are part of the namespace just like the package. That would guarantee uniqueness between two enums.

I should also mention that this is already happening for the C#.

Describe alternatives you've considered Enclose the protobuf enums into protobuff messages in order to provide better scoping. It is more messy in the import lines but the rest of the code stays clean and preserves the Java idioms.

Additional context Add any other context or screenshots about the feature request here. This feature request was already possible for C#, some context: https://github.com/protocolbuffers/protobuf/releases/tag/v3.0.0-beta-3 https://github.com/protocolbuffers/protobuf/issues/1079 https://github.com/protocolbuffers/protobuf/issues/1334

fioan89 avatar Jun 05 '24 14:06 fioan89

In Rust Prost it's already done like this. It would be a very useful addition.

ncioj10 avatar Aug 10 '24 13:08 ncioj10

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Nov 09 '24 10:11 github-actions[bot]

I'm still interested in this one

fioan89 avatar Nov 09 '24 11:11 fioan89

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Feb 08 '25 10:02 github-actions[bot]

I think this is a valid concern.

fioan89 avatar Feb 08 '25 10:02 fioan89

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Sep 21 '25 10:09 github-actions[bot]

Still a valid concern

fioan89 avatar Sep 21 '25 10:09 fioan89

This would be a great improvement. We do lots of mapping between protobuf objects and domain objects, and this is an unnecessary pain point.

Thanks!

internetstaff avatar Sep 21 '25 13:09 internetstaff

We can't just do this, since it's a major breaking change for any existing protos. However, we do have long-term plans to handle enum scoping better (i.e. remove the need for a redundant prefix in the first place), and this is on our radar. This will likely be handled with a new feature in a future edition

mkruskal-google avatar Oct 08 '25 05:10 mkruskal-google