Skip to content

Make EffectiveVariant.fromValue case-insensitive to match isValid - #14

Open
poketopa wants to merge 1 commit into
line:mainfrom
poketopa:bugfix/case-insensitive-effective-variant-lookup
Open

poketopa wants to merge 1 commit into
line:mainfrom
poketopa:bugfix/case-insensitive-effective-variant-lookup

Conversation

@poketopa

Copy link
Copy Markdown

Problem

EffectiveVariant.isValid and EffectiveVariant.fromValue disagree on case. isValid lower-cases its input before looking it up, so it accepts ON, True, Off, FALSE and so on, while fromValue looks the raw string up and only accepts the lower-case aliases. Both callers use them as a pair, isValid as the guard and fromValue right after it, so a mixed-case variant key passes the guard and then blows up in fromValue:

  • OpenFlagrProvider.getBooleanEvaluation passes the isValid check, then fromValue throws IllegalEnumValueException straight out of the provider. Every other failure in this provider is reported as a ProviderEvaluation with PARSE_ERROR; this is the only case where an exception escapes.
  • DefaultOpenFlagr.isFeatureFlagOn goes through .filter(EffectiveVariant::isValid).map(EffectiveVariant::fromValue), the exception is caught by the generic catch (Exception) and the method returns false with an Unexpected error. log. A flag whose variants are named ON/OFF in Flagr is therefore reported as off even when the evaluation returned ON.

Flagr accepts upper-case variant keys (^[\w\d-/\.\:]+$), so naming the variants ON/OFF in the Flagr UI is enough to hit this.

isValid(null) also throws NullPointerException instead of returning false.

The inconsistency is currently pinned by the tests: EffectiveVariantTest asserts both isValid("ON") == true and that fromValue("ON") throws.

Fix

Make fromValue normalize its input the same way isValid does, so the two methods agree on what a valid alias is. Both now go through a single normalize helper that uses toLowerCase(Locale.ROOT) (the existing isValid used the default-locale toLowerCase()). isValid returns false for null instead of throwing.

The exception message and getEnumValue() of IllegalEnumValueException still carry the original, un-normalized value.

I aligned fromValue to isValid rather than the other way round. isValid is public API and has been case-insensitive since the initial commit (the coverage tests from #10 also describe it as isValidAcceptsAliasesRegardlessOfCase), and a client that reads variant keys defined on the server side is better off accepting ON than rejecting it. Making both methods case-sensitive would be a stricter public API change and would still leave isValid(null) to fix. If you would rather keep the enum strict, I can flip the change to make isValid case-sensitive instead. OpenFlagrProvider and DefaultOpenFlagr are not touched; the enum change alone fixes both call sites.

The EffectiveVariant copy in flagship4j-javaflagr-spring-boot-web-starter is case-sensitive in both methods, so it is consistent and left untouched.

Tests

  • EffectiveVariantTest: the "ON" and "Off" cases move from fromValueRejectsUnknownValue to a new fromValueResolvesAliasesRegardlessOfCase, so the rejection test only covers genuinely unknown values. isValidRejectsUnknownValue gains a @NullSource case.
  • OpenFlagrProviderTest: booleanEvaluationMapsEffectiveVariantAliasesRegardlessOfCase checks that ON/True/Off/FALSE produce the right boolean with Reason.DEFAULT and no error code.
  • DefaultOpenFlagrTest: isFeatureFlagOnMapsEffectiveVariantAliasesRegardlessOfCase checks the same keys end to end through MockWebServer.

Before the fix, 14 of the new cases fail: IllegalEnumValueException: Cannot create enum from ON. in the enum and provider tests, NullPointerException for isValid(null), and isFeatureFlagOn returning false for ON/True. With the fix, ./gradlew clean build passes on JDK 8.

@CLAassistant

CLAassistant commented Oct 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@NoahHsu NoahHsu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@NoahHsu
NoahHsu requested review from JamHsu and fukuanru October 21, 2026 03:18
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.

3 participants