RelaxedConversionService support lowercase enums

Update RelaxedConversionService to support enums that are themselves
declared as lower-case (or mixed case) items.

Fixes gh-996
pull/1016/head
Phillip Webb 11 years ago
parent 97acd20d62
commit a947eb7660

@ -16,6 +16,9 @@
package org.springframework.boot.bind;
import java.util.EnumSet;
import java.util.Set;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
@ -117,7 +120,14 @@ class RelaxedConversionService implements ConversionService {
// It's an empty enum identifier: reset the enum value to null.
return null;
}
return (T) Enum.valueOf(this.enumType, source.trim().toUpperCase());
source = source.trim();
for (T candidate : (Set<T>) EnumSet.allOf(this.enumType)) {
if (candidate.name().equalsIgnoreCase(source)) {
return candidate;
}
}
throw new IllegalArgumentException("No enum constant "
+ this.enumType.getCanonicalName() + "." + source);
}
}

@ -442,7 +442,7 @@ public class RelaxedDataBinderTests {
result = bind(target, "bingo: oR");
assertThat(result.getErrorCount(), equalTo(0));
assertThat(target.getBingo(), equalTo(Bingo.OR));
assertThat(target.getBingo(), equalTo(Bingo.or));
result = bind(target, "bingo: that");
assertThat(result.getErrorCount(), equalTo(0));
@ -755,7 +755,7 @@ public class RelaxedDataBinderTests {
}
static enum Bingo {
THIS, OR, THAT
THIS, or, THAT
}
public static class ValidatedTarget {

Loading…
Cancel
Save