Skip to content

Auto-deduce valid values for enum @Option fields#7354

Merged
sambsnyd merged 1 commit intomainfrom
tim/option-enum-valid
Apr 14, 2026
Merged

Auto-deduce valid values for enum @Option fields#7354
sambsnyd merged 1 commit intomainfrom
tim/option-enum-valid

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

  • When an @Option annotation does not explicitly specify valid values and the annotated type is an enum, the valid values are now automatically populated from the enum constants via reflection.
  • Adds a private validValues() helper in Recipe.java used by all three option discovery paths (field, getter method, constructor parameter).
  • Explicit valid values on the annotation still take precedence over auto-deduction.

Test plan

  • Added tests for enum options on fields, getter methods, and constructor parameters
  • Added test verifying explicit valid takes precedence over enum constants
  • All existing RecipeBasicsTest tests continue to pass

When an @option annotation does not explicitly specify valid values and
the field type is an enum, automatically populate valid from the enum
constants via reflection.
@timtebeek
Copy link
Copy Markdown
Member Author

I noticed we're not always remembering to set the valid values correctly; e.g.

@Option(displayName = "Semver digit",
description = "`MAJOR` increments the first digit, `MINOR` increments the second digit, and `PATCH` " +
"increments the third digit.",
example = "PATCH")
SemverDigit digit;
public enum SemverDigit {
MAJOR,
MINOR,
PATCH
}

Which then results in an awkward text field:
image

This ensures we get all values exposed unless there's an explicit override.

@timtebeek timtebeek requested a review from sambsnyd April 12, 2026 16:38
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Apr 12, 2026
@timtebeek timtebeek added documentation Improvements or additions to documentation enhancement New feature or request labels Apr 12, 2026
@sambsnyd sambsnyd merged commit 7f60bc1 into main Apr 14, 2026
1 check passed
@sambsnyd sambsnyd deleted the tim/option-enum-valid branch April 14, 2026 23:43
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants