From a947eb7660055027e7406aa45e3c82dd3daa5cab Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 30 May 2014 16:28:19 +0100 Subject: [PATCH] RelaxedConversionService support lowercase enums Update RelaxedConversionService to support enums that are themselves declared as lower-case (or mixed case) items. Fixes gh-996 --- .../boot/bind/RelaxedConversionService.java | 12 +++++++++++- .../boot/bind/RelaxedDataBinderTests.java | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedConversionService.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedConversionService.java index 9129c3bad8..cbf1981bf1 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedConversionService.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedConversionService.java @@ -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) EnumSet.allOf(this.enumType)) { + if (candidate.name().equalsIgnoreCase(source)) { + return candidate; + } + } + throw new IllegalArgumentException("No enum constant " + + this.enumType.getCanonicalName() + "." + source); } } diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java index bb9e6b525a..1cb0dd4ddf 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java @@ -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 {