Spring version: 2.7.11

I found a problem with validation in Spring Webflux and spring-boot-starter-validation

When I try to validate a Mono object that has a nested Set, the validation doesn't work When I use the validation without nested generic it works

Example

Doesn't work, just passes with status 200 OK:

public Mono<String> greetings(@Valid @RequestBody Mono<Set<GreetingsRequest>> request) {
  //do something
}

Works:

public Mono<String> greetings(@Valid @RequestBody Mono<GreetingsRequest> request) {
  //do something
}

Works:

public Mono<String> greetings(@Valid @RequestBody Set<GreetingsRequest> request) {
  //do something
}

I found the same question on the StackOverflow: https://stackoverflow.com/questions/65353364/adding-javax-validation-to-map-type-request-body-in-spring-webflux But nobody resolved that since 2020

Comment From: rstoyanchev

I believe cascaded violations for container elements (List, Map) work only with method validation and with Object property validation. Method validation, however, cannot handle reactive or async types, so I don't think there is a way to make this work.

@Valid @RequestBody Mono<GreetingsRequest> request

This works because we validate the argument individually after it is emitted by the Mono, and for that bean validation supports cascaded validations on an Object graph.

@Valid @RequestBody Set<GreetingsRequest> request

This can work through method validation. Given Boot 2.7 you likely have @Validated at the class level? That applies method validation, or otherwise I don't see how else it works. Boot 3.2 (Spring Framework 6.1) has built-in method validation, and with that there is no longer a need for @Validated on the class level to apply method validation.

In summary this is expected behavior. You could wrap the set in a container object with an @Valid Set property, or ask the Hibernate Validator team if there is anything further they could do to support cascading on container elements when validating a value individually (i.e. not through method validation).

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.