Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-in support for standard bean validation on controller method arguments #24913

Closed
hedgerocker opened this issue Apr 16, 2020 · 3 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Comments

@hedgerocker
Copy link

hedgerocker commented Apr 16, 2020

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 16, 2020
@encircled
Copy link
Contributor

encircled commented Apr 16, 2020

Hi,

Use annotation @Validated instead of @Valid for @RequestBody parameter. Then MethodValidationPostProcessor will not be triggered and Spring arguments validation (with dependencies injection for validators) will still work.

@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Apr 16, 2020
@sbrannen sbrannen changed the title @Validated and @Valid triggers double validations. @Validated and @Valid trigger double validation Jul 5, 2022
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Jul 5, 2022

Team decision: we are going to provide first-class support for bean validation that's built at the web framework level (Spring MVC and WebFlux), without requiring a separate post-processor and an AOP proxy.

@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 5, 2022
@rstoyanchev rstoyanchev changed the title @Validated and @Valid trigger double validation Built-in support for standard bean validation on controller method arguments Jul 5, 2022
@rstoyanchev rstoyanchev added this to the 6.0.0-M6 milestone Jul 5, 2022
@jhoeller jhoeller modified the milestones: 6.0.0-M6, 6.0.0-RC1 Sep 12, 2022
@jhoeller jhoeller modified the milestones: 6.0.0-RC1, 6.x Backlog Oct 10, 2022
@rstoyanchev rstoyanchev modified the milestones: 6.x Backlog, 6.1.x Dec 9, 2022
@rstoyanchev rstoyanchev self-assigned this Dec 9, 2022
@rstoyanchev rstoyanchev changed the title Built-in support for standard bean validation on controller method arguments Built-in Web Support for Method Bean Validation Jan 16, 2023
@rstoyanchev rstoyanchev modified the milestones: 6.1.x, 6.1.0-M1 Jan 16, 2023
@rstoyanchev rstoyanchev changed the title Built-in Web Support for Method Bean Validation Built-in support for standard bean validation on controller method arguments Jan 16, 2023
@rstoyanchev
Copy link
Contributor

I'm closing this in favor of #29825 that summarizes issues with validation across multiple issues, and also highlights some of the design questions. Please take a look and provide further feedback if necessary under the new issue.

@rstoyanchev rstoyanchev closed this as not planned Won't fix, can't repro, duplicate, stale Jan 16, 2023
@rstoyanchev rstoyanchev removed this from the 6.1.0-M1 milestone Jan 16, 2023
@rstoyanchev rstoyanchev added the status: superseded An issue that has been superseded by another label Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants