Affects: 6.0.11
I am using Spring WebFlux and generating Controller APIs using openapi generator. I have defined a @jakarta.validation.constraints.Pattern
constraint on my request parameter which is a String
. It seems that the constraint is not supported.
@RequestMapping(
method = RequestMethod.PUT,
value = "/resource",
produces = { "application/json" },
consumes = { "application/json" }
)
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> updateRessource(@Parameter(name = "body", description = "") @Valid @Pattern(regexp = "[A-zÀ-ú0-9_\\-/:%'@]*") @Size(max = 200) @RequestBody(required = false) Mono<String> newValue, ServerWebExchange exchange)
I get the following error when my endpoint is invoked:
jakarta.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'jakarta.validation.constraints.Pattern' validating type 'reactor.core.publisher.Mono
'
According to the documentation, it seems that only @Valid
or @Validated
are supported.
I think other constraints should be supported too.
Comment From: sbrannen
I have defined a
@jakarta.validation.constraints.Pattern
constraint on my request parameter which is aString
.
It might not make a difference, but your request parameter is neither a String
nor a primitive type but rather a Mono<String>
.
Comment From: ghilainm
@sbrannen It works properly when it is a Mono of non-primitive types :).
Comment From: rstoyanchev
The only way to validate constraints directly on method parameters is to use the method validation feature of the Bean Validation API, i.e. passing the array of arguments to Hibernate Validator, and that does not support reactive types. By contrast, for @Valid
we can validate an object via Bean Validation, and that's what allows us to support Mono<T>
with a non-primitive type in WebFlux.
In short, this is expected behavior.