|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2023 the original author or authors. |
| 2 | + * Copyright 2012-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import java.lang.annotation.Retention;
|
22 | 22 | import java.lang.annotation.RetentionPolicy;
|
23 | 23 | import java.lang.annotation.Target;
|
| 24 | +import java.lang.reflect.Parameter; |
24 | 25 | import java.net.URI;
|
25 | 26 | import java.util.ArrayList;
|
26 | 27 | import java.util.Arrays;
|
|
46 | 47 | import org.springframework.context.ConfigurableApplicationContext;
|
47 | 48 | import org.springframework.context.annotation.Bean;
|
48 | 49 | import org.springframework.context.annotation.Configuration;
|
| 50 | +import org.springframework.core.MethodParameter; |
49 | 51 | import org.springframework.http.HttpStatus;
|
50 | 52 | import org.springframework.http.MediaType;
|
51 | 53 | import org.springframework.http.RequestEntity;
|
52 | 54 | import org.springframework.http.ResponseEntity;
|
| 55 | +import org.springframework.util.ReflectionUtils; |
53 | 56 | import org.springframework.util.StringUtils;
|
54 | 57 | import org.springframework.validation.BindException;
|
55 | 58 | import org.springframework.web.bind.MethodArgumentNotValidException;
|
56 | 59 | import org.springframework.web.bind.annotation.PostMapping;
|
| 60 | +import org.springframework.web.bind.annotation.RequestAttribute; |
57 | 61 | import org.springframework.web.bind.annotation.RequestBody;
|
58 | 62 | import org.springframework.web.bind.annotation.RequestMapping;
|
59 | 63 | import org.springframework.web.bind.annotation.ResponseStatus;
|
@@ -260,29 +264,32 @@ void testBindingExceptionForMachineClientNeverMessage() {
|
260 | 264 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
261 | 265 | private void bindingExceptionWithErrors(String param) {
|
262 | 266 | 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"); |
264 | 269 | assertThat(entity.getBody()).containsKey("errors");
|
265 | 270 | }
|
266 | 271 |
|
267 | 272 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
268 | 273 | private void bindingExceptionWithoutErrors(String param) {
|
269 | 274 | 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"); |
271 | 277 | assertThat(entity.getBody()).doesNotContainKey("errors");
|
272 | 278 | }
|
273 | 279 |
|
274 | 280 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
275 | 281 | private void bindingExceptionWithMessage(String param) {
|
276 | 282 | 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, |
278 | 284 | "Validation failed for object='test'. Error count: 1", "/bind");
|
279 | 285 | assertThat(entity.getBody()).doesNotContainKey("errors");
|
280 | 286 | }
|
281 | 287 |
|
282 | 288 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
283 | 289 | private void bindingExceptionWithoutMessage(String param) {
|
284 | 290 | 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"); |
286 | 293 | assertThat(entity.getBody()).doesNotContainKey("errors");
|
287 | 294 | }
|
288 | 295 |
|
@@ -428,10 +435,12 @@ String annotatedNoMessage() {
|
428 | 435 | }
|
429 | 436 |
|
430 | 437 | @RequestMapping("/bind")
|
431 |
| - String bind() throws Exception { |
| 438 | + String bind(@RequestAttribute(required = false) String foo) throws Exception { |
432 | 439 | BindException error = new BindException(this, "test");
|
433 | 440 | 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); |
435 | 444 | }
|
436 | 445 |
|
437 | 446 | @PostMapping(path = "/bodyValidation", produces = "application/json")
|
|
0 commit comments