Add BindErrorUtils
This deprecates static methods in MethodArgumentNotValidException which is not a great vehicle for such methods. See gh-30644
This commit is contained in:
committed by
rstoyanchev
parent
e83594a2a3
commit
ba4d9a5230
@@ -56,6 +56,7 @@ import org.springframework.web.server.ServerErrorException;
|
||||
import org.springframework.web.server.UnsatisfiedRequestParameterException;
|
||||
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
|
||||
import org.springframework.web.testfixture.method.ResolvableMethod;
|
||||
import org.springframework.web.util.BindErrorUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -252,7 +253,8 @@ public class ErrorResponseExceptionTests {
|
||||
assertStatus(ex, HttpStatus.BAD_REQUEST);
|
||||
assertDetail(ex, "Invalid request content.");
|
||||
messageSourceHelper.assertDetailMessage(ex);
|
||||
messageSourceHelper.assertErrorMessages(ex::resolveErrorMessages);
|
||||
messageSourceHelper.assertErrorMessages(
|
||||
(source, locale) -> BindErrorUtils.resolve(ex.getAllErrors(), source, locale));
|
||||
|
||||
assertThat(ex.getHeaders()).isEmpty();
|
||||
}
|
||||
@@ -457,8 +459,8 @@ public class ErrorResponseExceptionTests {
|
||||
ex.getDetailMessageCode(), ex.getDetailMessageArguments(), Locale.UK);
|
||||
|
||||
assertThat(message).isEqualTo(
|
||||
"Failed because Invalid bean message, and bean.invalid.B. " +
|
||||
"Also because name: must be provided, and age: age.min");
|
||||
"Failed because Invalid bean message, and bean.invalid.B.myBean. " +
|
||||
"Also because name: must be provided, and age: age.min.myBean.age");
|
||||
|
||||
message = messageSource.getMessage(
|
||||
ex.getDetailMessageCode(), ex.getDetailMessageArguments(messageSource, Locale.UK), Locale.UK);
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.web.bind;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
@@ -29,9 +29,9 @@ import org.springframework.context.support.StaticMessageSource;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
|
||||
import org.springframework.web.util.BindErrorUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -47,8 +47,7 @@ public class MethodArgumentNotValidExceptionTests {
|
||||
Person frederick1234 = new Person("Frederick1234", 24);
|
||||
MethodArgumentNotValidException ex = createException(frederick1234);
|
||||
|
||||
List<FieldError> fieldErrors = ex.getFieldErrors();
|
||||
List<String> errors = MethodArgumentNotValidException.errorsToStringList(fieldErrors);
|
||||
Collection<String> errors = BindErrorUtils.resolve(ex.getFieldErrors()).values();
|
||||
|
||||
assertThat(errors).containsExactlyInAnyOrder(
|
||||
"name: size must be between 0 and 10", "age: must be greater than or equal to 25");
|
||||
@@ -63,8 +62,7 @@ public class MethodArgumentNotValidExceptionTests {
|
||||
source.addMessage("Size.name", Locale.UK, "name exceeds {1} characters");
|
||||
source.addMessage("Min.age", Locale.UK, "age is under {1}");
|
||||
|
||||
List<FieldError> fieldErrors = ex.getFieldErrors();
|
||||
List<String> errors = MethodArgumentNotValidException.errorsToStringList(fieldErrors, source, Locale.UK);
|
||||
Collection<String> errors = BindErrorUtils.resolve(ex.getFieldErrors(), source, Locale.UK).values();
|
||||
|
||||
assertThat(errors).containsExactlyInAnyOrder("name exceeds 10 characters", "age is under 25");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user