Skip to content

Commit 460d8a0

Browse files
Merge branch '3.1.x' into 3.2.x
Closes gh-39773
2 parents 83efe76 + e67e6bb commit 460d8a0

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
24+
import java.lang.reflect.Parameter;
2425
import java.net.URI;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
@@ -46,14 +47,17 @@
4647
import org.springframework.context.ConfigurableApplicationContext;
4748
import org.springframework.context.annotation.Bean;
4849
import org.springframework.context.annotation.Configuration;
50+
import org.springframework.core.MethodParameter;
4951
import org.springframework.http.HttpStatus;
5052
import org.springframework.http.MediaType;
5153
import org.springframework.http.RequestEntity;
5254
import org.springframework.http.ResponseEntity;
55+
import org.springframework.util.ReflectionUtils;
5356
import org.springframework.util.StringUtils;
5457
import org.springframework.validation.BindException;
5558
import org.springframework.web.bind.MethodArgumentNotValidException;
5659
import org.springframework.web.bind.annotation.PostMapping;
60+
import org.springframework.web.bind.annotation.RequestAttribute;
5761
import org.springframework.web.bind.annotation.RequestBody;
5862
import org.springframework.web.bind.annotation.RequestMapping;
5963
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -260,29 +264,32 @@ void testBindingExceptionForMachineClientNeverMessage() {
260264
@SuppressWarnings({ "rawtypes", "unchecked" })
261265
private void bindingExceptionWithErrors(String param) {
262266
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
263-
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, null, "/bind");
267+
assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, null,
268+
"/bind");
264269
assertThat(entity.getBody()).containsKey("errors");
265270
}
266271

267272
@SuppressWarnings({ "rawtypes", "unchecked" })
268273
private void bindingExceptionWithoutErrors(String param) {
269274
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
270-
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, null, "/bind");
275+
assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, null,
276+
"/bind");
271277
assertThat(entity.getBody()).doesNotContainKey("errors");
272278
}
273279

274280
@SuppressWarnings({ "rawtypes", "unchecked" })
275281
private void bindingExceptionWithMessage(String param) {
276282
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
277-
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class,
283+
assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class,
278284
"Validation failed for object='test'. Error count: 1", "/bind");
279285
assertThat(entity.getBody()).doesNotContainKey("errors");
280286
}
281287

282288
@SuppressWarnings({ "rawtypes", "unchecked" })
283289
private void bindingExceptionWithoutMessage(String param) {
284290
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
285-
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, null, "/bind");
291+
assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, null,
292+
"/bind");
286293
assertThat(entity.getBody()).doesNotContainKey("errors");
287294
}
288295

@@ -428,10 +435,12 @@ String annotatedNoMessage() {
428435
}
429436

430437
@RequestMapping("/bind")
431-
String bind() throws Exception {
438+
String bind(@RequestAttribute(required = false) String foo) throws Exception {
432439
BindException error = new BindException(this, "test");
433440
error.rejectValue("foo", "bar.error");
434-
throw error;
441+
Parameter fooParameter = ReflectionUtils.findMethod(Errors.class, "bind", String.class)
442+
.getParameters()[0];
443+
throw new MethodArgumentNotValidException(MethodParameter.forParameter(fooParameter), error);
435444
}
436445

437446
@PostMapping(path = "/bodyValidation", produces = "application/json")

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerMockMvcTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
24+
import java.lang.reflect.Parameter;
2425
import java.util.Map;
2526

2627
import jakarta.servlet.DispatcherType;
@@ -41,6 +42,7 @@
4142
import org.springframework.boot.test.context.SpringBootTest;
4243
import org.springframework.context.annotation.Bean;
4344
import org.springframework.context.annotation.Configuration;
45+
import org.springframework.core.MethodParameter;
4446
import org.springframework.http.HttpStatus;
4547
import org.springframework.http.MediaType;
4648
import org.springframework.mock.web.MockHttpServletRequest;
@@ -49,7 +51,10 @@
4951
import org.springframework.test.web.servlet.MvcResult;
5052
import org.springframework.test.web.servlet.RequestBuilder;
5153
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
54+
import org.springframework.util.ReflectionUtils;
5255
import org.springframework.validation.BindException;
56+
import org.springframework.web.bind.MethodArgumentNotValidException;
57+
import org.springframework.web.bind.annotation.RequestAttribute;
5358
import org.springframework.web.bind.annotation.RequestMapping;
5459
import org.springframework.web.bind.annotation.ResponseStatus;
5560
import org.springframework.web.bind.annotation.RestController;
@@ -175,10 +180,12 @@ String bang() {
175180
}
176181

177182
@RequestMapping("/bind")
178-
String bind() throws Exception {
183+
String bind(@RequestAttribute(required = false) String foo) throws Exception {
179184
BindException error = new BindException(this, "test");
180185
error.rejectValue("foo", "bar.error");
181-
throw error;
186+
Parameter fooParameter = ReflectionUtils.findMethod(Errors.class, "bind", String.class)
187+
.getParameters()[0];
188+
throw new MethodArgumentNotValidException(MethodParameter.forParameter(fooParameter), error);
182189
}
183190

184191
@RequestMapping("/noContent")

0 commit comments

Comments
 (0)