mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Improve relaxed enum binding
Update RelaxedConversionService to support more relaxed enum binding. Fixes gh-1950
This commit is contained in:
parent
f43d6925c5
commit
4668f59723
@ -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");
|
||||
}
|
||||
|
||||
}
|
@ -123,6 +123,13 @@ class RelaxedConversionService implements ConversionService {
|
||||
}
|
||||
source = source.trim();
|
||||
for (T candidate : (Set<T>) 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;
|
||||
}
|
||||
|
@ -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<Bingo, Object> nested;
|
||||
|
||||
public Map<Bingo, Object> getNested() {
|
||||
return nested;
|
||||
return this.nested;
|
||||
}
|
||||
|
||||
public void setNested(Map<Bingo, Object> nested) {
|
||||
@ -800,7 +812,7 @@ public class RelaxedDataBinderTests {
|
||||
}
|
||||
|
||||
static enum Bingo {
|
||||
THIS, or, THAT
|
||||
THIS, or, THAT, THE_OTHER
|
||||
}
|
||||
|
||||
public static class ValidatedTarget {
|
||||
|
Loading…
Reference in New Issue
Block a user