From 4668f597231ca4e6a1bf5852f3f835636cb69290 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 18 Nov 2014 11:24:32 -0800 Subject: [PATCH] Improve relaxed enum binding Update RelaxedConversionService to support more relaxed enum binding. Fixes gh-1950 --- .../tomcat/web/SampleControllerAdvice.java | 37 +++++++++++++++++++ .../boot/bind/RelaxedConversionService.java | 7 ++++ .../boot/bind/RelaxedDataBinderTests.java | 16 +++++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 spring-boot-samples/spring-boot-sample-tomcat/src/main/java/sample/tomcat/web/SampleControllerAdvice.java diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/main/java/sample/tomcat/web/SampleControllerAdvice.java b/spring-boot-samples/spring-boot-sample-tomcat/src/main/java/sample/tomcat/web/SampleControllerAdvice.java new file mode 100644 index 0000000000..a725e7c7a6 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/main/java/sample/tomcat/web/SampleControllerAdvice.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.tomcat.web; + +import javax.servlet.DispatcherType; +import javax.servlet.http.HttpServletRequest; + +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ModelAttribute; + +@ControllerAdvice +public class SampleControllerAdvice { + + @ModelAttribute + public boolean test(HttpServletRequest request) { + if (DispatcherType.ERROR.equals(request.getDispatcherType())) { + return false; + } + System.out.println(request.getPathInfo()); + throw new RuntimeException("test"); + } + +} 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 a645e94cb8..ff189199fb 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 @@ -123,6 +123,13 @@ class RelaxedConversionService implements ConversionService { } source = source.trim(); for (T candidate : (Set) EnumSet.allOf(this.enumType)) { + RelaxedNames names = new RelaxedNames(candidate.name() + .replace("_", "-").toLowerCase()); + for (String name : names) { + if (name.equals(source)) { + return candidate; + } + } if (candidate.name().equalsIgnoreCase(source)) { return candidate; } 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 c0f67e3ef5..50729be019 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 @@ -479,6 +479,18 @@ public class RelaxedDataBinderTests { result = bind(target, "bingo: that"); assertThat(result.getErrorCount(), equalTo(0)); assertThat(target.getBingo(), equalTo(Bingo.THAT)); + + result = bind(target, "bingo: the-other"); + assertThat(result.getErrorCount(), equalTo(0)); + assertThat(target.getBingo(), equalTo(Bingo.THE_OTHER)); + + result = bind(target, "bingo: the_other"); + assertThat(result.getErrorCount(), equalTo(0)); + assertThat(target.getBingo(), equalTo(Bingo.THE_OTHER)); + + result = bind(target, "bingo: The_Other"); + assertThat(result.getErrorCount(), equalTo(0)); + assertThat(target.getBingo(), equalTo(Bingo.THE_OTHER)); } private BindingResult bind(Object target, String values) throws Exception { @@ -615,7 +627,7 @@ public class RelaxedDataBinderTests { private Map nested; public Map getNested() { - return nested; + return this.nested; } public void setNested(Map nested) { @@ -800,7 +812,7 @@ public class RelaxedDataBinderTests { } static enum Bingo { - THIS, or, THAT + THIS, or, THAT, THE_OTHER } public static class ValidatedTarget {