Closed
Description
Frédéric Camblor opened SPR-13428 and commented
Putting a @Validated
annotation on @RequestBody
of an Iterable (or any implementation such as List) doesn't trigger any bean validation process
To reproduce, take the following controller declaration :
@Controller
public class HelloWorldController {
public static class Hello {
@NotNull
private String who;
public String getWho() { return who; }
public void setWho(String who) { this.who = who; }
}
// If I send JSON : {}
// => Validation error about "who" field being mandatory (it's ok)
@RequestMapping(value="/testWithPOJO", method=RequestMethod.POST)
public @ResponseBody List<Hello> testPost5(@RequestBody @Validated Hello hello) {
return Arrays.asList(hello);
}
// If I send JSON : [ {} ]
// => No Validation error, bean validation is not triggered on instances, this looks weird
@RequestMapping(value="/testWithArray", method=RequestMethod.PUT)
public @ResponseBody List<Hello> testPost1(@RequestBody @Validated List<Hello> hellos) {
return hellos;
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(value = HttpStatus.PRECONDITION_FAILED)
public @ResponseBody List<ObjectError> handleValidationFailure(MethodArgumentNotValidException exception) {
return exception.getBindingResult().getAllErrors();
}
}
WDYT ?
Affects: 3.2 GA, 4.0 GA, 4.1 GA
Issue Links:
- Bean Validation invocation API for use with individual values and constraints [SPR-11900] #16519 Bean Validation invocation API for use with individual values and constraints
- Support validating collection of objects on web controller method #16917 Add support for validating a collection of objects
- Validate values in top-level Map parameters [SPR-14615] #19182 Validate values in top-level Map parameters
2 votes, 3 watchers