Skip to content

feat: support unrecognized enum values#693

Merged
anthony-swirldslabs merged 1 commit intomainfrom
476-unrecognizedEnum
Dec 11, 2025
Merged

feat: support unrecognized enum values#693
anthony-swirldslabs merged 1 commit intomainfrom
476-unrecognizedEnum

Conversation

@anthony-swirldslabs
Copy link
Copy Markdown
Contributor

Description:
Introducing support for unrecognized enum values.

  • Generated non-oneOf enums have a new UNRECOGNIZED(-1) enum constant added
  • Model's enumField() returns UNRECOGNIZED for unknown proto ordinals
  • A new model's int enumFieldProtoOrdinal() returns the protoOrdinal as parsed from input
  • Enum lists have List<Integer> enumListFieldProtoOrdinals() similar to the above
  • Serializing/deserializing preserves unrecognized enum values, which is verified with a new integration test.
  • New constructors are added to models and builders to support the new feature (with Object/List<?>).

The implementation avoids increasing the memory footprint of generated protobuf models by replacing the real enum type with Object/List<?> for the private fields that store these values. In other words, with this fix, Java models still maintain just a single reference field for each protobuf enum field - same as before. The Object can be either an actual enum constant or an Integer representing its unrecognized protoOrdinal value.

Getters for repeated enum fields (both simple and ...ProtoOrdinals()) are penalized slightly because they have to construct new lists each time. As a future work, we could consider caching the results in runtime similar to how we do with the hashCode/length. However, this would increase the memory foot print for such models. For this reason, I haven't implemented this just yet. Note that non-repeated enum fields are unaffected by this at all and should work as fast as they do today.

Related issue(s):

Fixes #476

Notes for reviewer:
A new integration test is added to verify the behavior.

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
@github-actions
Copy link
Copy Markdown

JUnit Test Report

   78 files  ±0     78 suites  ±0   4m 45s ⏱️ +59s
1 350 tests ±0  1 346 ✅ ±0   4 💤 ±0  0 ❌ ±0 
7 227 runs  ±0  7 207 ✅ ±0  20 💤 ±0  0 ❌ ±0 

Results for commit 7bb827b. ± Comparison against base commit 6c0c316.

@github-actions
Copy link
Copy Markdown

Integration Test Report

    413 files  + 3      413 suites  +3   17m 24s ⏱️ + 1m 1s
114 874 tests +13  114 874 ✅ +13  0 💤 ±0  0 ❌ ±0 
115 115 runs  +13  115 115 ✅ +13  0 💤 ±0  0 ❌ ±0 

Results for commit 7bb827b. ± Comparison against base commit 6c0c316.

@anthony-swirldslabs anthony-swirldslabs merged commit 9b7a491 into main Dec 11, 2025
12 checks passed
@anthony-swirldslabs anthony-swirldslabs deleted the 476-unrecognizedEnum branch December 11, 2025 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support unrecognized enum values

2 participants