Update error format in MethodArgumentNotValidException
1. Remove list markers (those can be provided in message). 2. Use ", and " between errors for readability. 3. Remove single quotes around errors. 4. If MessageSource is provided, use resolved message as is since in that case applications have full control over each message. Closes gh-30198
This commit is contained in:
@@ -444,7 +444,7 @@ public class ErrorResponseExceptionTests {
|
||||
BindingResult bindingResult = new BindException(new TestBean(), "myBean");
|
||||
bindingResult.reject("bean.invalid.A", "Invalid bean message");
|
||||
bindingResult.reject("bean.invalid.B");
|
||||
bindingResult.rejectValue("name", "name.required", "Name is required");
|
||||
bindingResult.rejectValue("name", "name.required", "must be provided");
|
||||
bindingResult.rejectValue("age", "age.min");
|
||||
return bindingResult;
|
||||
}
|
||||
@@ -456,16 +456,16 @@ public class ErrorResponseExceptionTests {
|
||||
String message = messageSource.getMessage(
|
||||
ex.getDetailMessageCode(), ex.getDetailMessageArguments(), Locale.UK);
|
||||
|
||||
assertThat(message).isEqualTo("" +
|
||||
"Failures ['Invalid bean message', 'bean.invalid.B']. " +
|
||||
"nested failures: [name: 'Name is required', age: 'age.min']");
|
||||
assertThat(message).isEqualTo(
|
||||
"Failed because Invalid bean message, and bean.invalid.B. " +
|
||||
"Also because name: must be provided, and age: age.min");
|
||||
|
||||
message = messageSource.getMessage(
|
||||
ex.getDetailMessageCode(), ex.getDetailMessageArguments(messageSource, Locale.UK), Locale.UK);
|
||||
|
||||
assertThat(message).isEqualTo("" +
|
||||
"Failures ['Bean A message', 'Bean B message']. " +
|
||||
"nested failures: [name: 'Required name message', age: 'Minimum age message']");
|
||||
assertThat(message).isEqualTo(
|
||||
"Failed because Bean A message, and Bean B message. " +
|
||||
"Also because name is required, and age is below minimum");
|
||||
}
|
||||
|
||||
private void assertErrorMessages(BiFunction<MessageSource, Locale, Map<ObjectError, String>> expectedMessages) {
|
||||
@@ -473,16 +473,16 @@ public class ErrorResponseExceptionTests {
|
||||
Map<ObjectError, String> map = expectedMessages.apply(messageSource, Locale.UK);
|
||||
|
||||
assertThat(map).hasSize(4).containsValues(
|
||||
"'Bean A message'", "'Bean B message'", "name: 'Required name message'", "age: 'Minimum age message'");
|
||||
"Bean A message", "Bean B message", "name is required", "age is below minimum");
|
||||
}
|
||||
|
||||
private StaticMessageSource initMessageSource() {
|
||||
StaticMessageSource messageSource = new StaticMessageSource();
|
||||
messageSource.addMessage(this.code, Locale.UK, "Failures {0}. nested failures: {1}");
|
||||
messageSource.addMessage(this.code, Locale.UK, "Failed because {0}. Also because {1}");
|
||||
messageSource.addMessage("bean.invalid.A", Locale.UK, "Bean A message");
|
||||
messageSource.addMessage("bean.invalid.B", Locale.UK, "Bean B message");
|
||||
messageSource.addMessage("name.required", Locale.UK, "Required name message");
|
||||
messageSource.addMessage("age.min", Locale.UK, "Minimum age message");
|
||||
messageSource.addMessage("name.required", Locale.UK, "name is required");
|
||||
messageSource.addMessage("age.min", Locale.UK, "age is below minimum");
|
||||
return messageSource;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2002-2023 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
|
||||
*
|
||||
* https://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 org.springframework.web.bind;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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 static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MethodArgumentNotValidException}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MethodArgumentNotValidExceptionTests {
|
||||
|
||||
@Test
|
||||
void errorsToStringList() throws Exception {
|
||||
Person frederick1234 = new Person("Frederick1234", 24);
|
||||
MethodArgumentNotValidException ex = createException(frederick1234);
|
||||
|
||||
List<FieldError> fieldErrors = ex.getFieldErrors();
|
||||
List<String> errors = MethodArgumentNotValidException.errorsToStringList(fieldErrors);
|
||||
|
||||
assertThat(errors).containsExactlyInAnyOrder(
|
||||
"name: size must be between 0 and 10", "age: must be greater than or equal to 25");
|
||||
}
|
||||
|
||||
@Test
|
||||
void errorsToStringListWithMessageSource() throws Exception {
|
||||
Person frederick1234 = new Person("Frederick1234", 24);
|
||||
MethodArgumentNotValidException ex = createException(frederick1234);
|
||||
|
||||
StaticMessageSource source = new StaticMessageSource();
|
||||
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);
|
||||
|
||||
assertThat(errors).containsExactlyInAnyOrder("name exceeds 10 characters", "age is under 25");
|
||||
}
|
||||
|
||||
private static MethodArgumentNotValidException createException(Person person) throws Exception {
|
||||
LocalValidatorFactoryBean validatorBean = new LocalValidatorFactoryBean();
|
||||
validatorBean.afterPropertiesSet();
|
||||
SpringValidatorAdapter validator = new SpringValidatorAdapter(validatorBean);
|
||||
|
||||
BindingResult result = new BeanPropertyBindingResult(person, "person");
|
||||
validator.validate(person, result);
|
||||
|
||||
Method method = Handler.class.getDeclaredMethod("handle", Person.class);
|
||||
MethodParameter parameter = new MethodParameter(method, 0);
|
||||
|
||||
return new MethodArgumentNotValidException(parameter, result);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class Handler {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
void handle(Person person) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private record Person(@Size(max = 10) String name, @Min(25) int age) {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user