-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
48548b2
commit 96c494c
Showing
5 changed files
with
149 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...-web/src/test/java/org/springframework/web/bind/MethodArgumentNotValidExceptionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
} | ||
|
||
} |