Skip to content

Commit 846167c

Browse files
committed
Merge branch '3.3.x'
Closes spring-projectsgh-41988
2 parents 8550e56 + 8bd827e commit 846167c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ else if (error instanceof MethodValidationResult methodValidationResult) {
123123
else if (error instanceof ResponseStatusException responseStatusException) {
124124
errorAttributes.put("message", responseStatusException.getReason());
125125
exception = (responseStatusException.getCause() != null) ? responseStatusException.getCause() : error;
126+
if (exception instanceof BindingResult bindingResult) {
127+
errorAttributes.put("errors", bindingResult.getAllErrors());
128+
}
126129
}
127130
else {
128131
exception = error;

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ void extractBindingResultErrors() throws Exception {
275275
assertThat(attributes).containsEntry("errors", bindingResult.getAllErrors());
276276
}
277277

278+
@Test
279+
void extractBindingResultErrorsThatCausedAResponseStatusException() throws Exception {
280+
Method method = getClass().getDeclaredMethod("method", String.class);
281+
MethodParameter stringParam = new MethodParameter(method, 0);
282+
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
283+
bindingResult.addError(new ObjectError("c", "d"));
284+
Exception ex = new WebExchangeBindException(stringParam, bindingResult);
285+
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
286+
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(
287+
buildServerRequest(request, new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid", ex)),
288+
ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
289+
assertThat(attributes.get("message")).isEqualTo("Invalid");
290+
assertThat(attributes).containsEntry("errors", bindingResult.getAllErrors());
291+
}
292+
278293
@Test
279294
void extractMethodValidationResultErrors() throws Exception {
280295
Object target = "test";

0 commit comments

Comments
 (0)