-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Closed as not planned
Closed as not planned
Copy link
Labels
in: webIssues in web modules (web, webmvc, webflux, websocket)Issues in web modules (web, webmvc, webflux, websocket)status: supersededAn issue that has been superseded by anotherAn issue that has been superseded by anothertype: enhancementA general enhancementA general enhancement
Description
Related to #11041.
I'm trying to validate @PathVariable
and @RequestBody
in one method. MethodValidationPostProcessor
is registered. Simplified Controller looks like this:
@RestController
@Validated
public class MyController {
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public ResponseEntity method_name(@PathVariable @NotNull String id, @Valid @RequestBody Body body) {
/// Some code
}
}
I noticed that request body validation is triggered twice - it wouldn't be an issue if it weren't custom ConstraintValidator
with injected bean:
public class CustomValidator implements ConstraintValidator<Body, String> {
@Autowired
private BodyVerificator bodyVerificator;
...
For first validation run the bean is injected properly, and for second run it's null so it results in NPE. Workaround is to either not use method parameter validation at all, or to annotate @RequestBody
with @Validated
so not mix @Validated
and @Valid
. I would actually prefer to have @PathVariable
bean validation support in Spring like in #11041.
lkopicki, macxq, rafzem, timeros07, kchrusciel and 7 more
Metadata
Metadata
Assignees
Labels
in: webIssues in web modules (web, webmvc, webflux, websocket)Issues in web modules (web, webmvc, webflux, websocket)status: supersededAn issue that has been superseded by anotherAn issue that has been superseded by anothertype: enhancementA general enhancementA general enhancement