Skip to content

@Validated support on Iterables (and implementors) [SPR-13428] #18007

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

2 votes, 3 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issuetype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions