protocolbuffers / protocolbuffers/protobuf

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

Open
#17,040 12 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

editions enhancement java keep open
Dominant language
C++
Stars
72k
Forks
16.3k
Avg merge
1d 17h
Merged PRs (30d)
140

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing Java enum-generation behavior and the referenced C# precedent, including issues #1079 and #1334. Then inspect the related Java generator tests; done means proto3 enums with a matching type-name prefix produce usable Java enum values without that prefix while preserving existing behavior where it does not match.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.