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

Spring MVC and WebFlux method validation support that also adapts ConstraintViolations #29825

Closed
Tracked by #30645
rstoyanchev opened this issue Jan 16, 2023 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Jan 16, 2023

Introduce support for method validation in Spring MVC and Spring WebFlux. This requires changes first at a lower level to enhance MethodValidationInterceptor infrastructure to adapt ConstraintViolations to MessageSourceResolvable errors, further aggregated into a BindingResult for cascaded violations on Object method parameters.

For the full scope of changes on this theme, see #30645.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Jan 16, 2023
@rstoyanchev rstoyanchev added this to the 6.1.0-M1 milestone Jan 16, 2023
@rstoyanchev rstoyanchev self-assigned this Jan 16, 2023
@neroncap
Copy link

Hi! I'm interesting in make some custom validations for the entire @RequestHeader, i was testing this issue and is not possible. It is planned to be included in further versions?

@rstoyanchev
Copy link
Contributor Author

rstoyanchev commented Jan 23, 2023

Yes it is possible as indicated in the summary above, but the goal is to make it better integrated with Spring MVC and WebFlux, and easier to handle validation exceptions.

If you need help to make it work as it is currently, this is not the right place.

rstoyanchev added a commit that referenced this issue Jun 12, 2023
rstoyanchev added a commit that referenced this issue Jun 12, 2023
rstoyanchev added a commit that referenced this issue Jun 12, 2023
rstoyanchev added a commit that referenced this issue Jun 12, 2023
rstoyanchev added a commit that referenced this issue Jun 12, 2023
@rstoyanchev rstoyanchev changed the title Built-in Web Support for Method Bean Validation Spring MVC and WebFlux method validation support that also adapts ConstraintViolations Jun 12, 2023
@rstoyanchev
Copy link
Contributor Author

rstoyanchev commented Jun 12, 2023

This issue has been re-purposed to match the initial support in 6.1 M1, and the changes for that are now available in 6.1.0-SNAPSHOT. Below is a brief summary of the changes, ahead of a more detailed documentation update in #30643.

spring-context changes

MethodValidationInterceptor now delegates to MethodValidationAdapter, comparable to SpringValidatorAdapter but for method validation. It can return a MethodValidationResult with ConstraintViolations adapted to ParameterValidationResult each containing a list of MessageSourceResolvable errors, also grouped by method parameter, or raise MethodValidationException with the same results. A ParameterValidationResult may also be ParameterErrors, an Errors instance with cascaded violations adapted to field errors.

Spring MVC and WebFlux changes

Spring MVC and WebFlux have been updated to use MethodValidationAdater to apply method validation, and raise an MethodValidationException in case of validation errors. In order to make use of it, remove @Validated on the class level (i.e. opt out of AOP method validation), ensure jakarta.validation.Validator such as LocalValidatorFactoryBean is configured for use in Spring MVC or WebFlux config, and then it should apply to controller methods @Constraint annotations.

Controller methods that have only @Valid parameters, but otherwise no @Constraint annotations directly on the controller method, continue to be validated as they were before by the argument resolver, e.g. leading to MethodArgumentNotValidException. It's only when there are @Constraint annotations that method validation applies, and in that case the argument resolver will not validate and instead defer to method Bean Validation. BindingResult arguments work just the same with method validation. However, in case of additional constraint violations, the method cannot be invoked and MethodValidationException is thrown instead.

@rstoyanchev
Copy link
Contributor Author

I'm closing this issue as complete. Trying out the changes for 6.1 M1 with an actual application would be much appreciated. In case of feedback, please comment under the umbrella issue #30645, and we'll decide there how to proceed. Also subscribe to to watch the umbrella issue for further updates.

rstoyanchev added a commit that referenced this issue Jun 13, 2023
This allows re-use of existing MethodParameter instances from controller
methods with cached metadata, and also ensures additional capabilities
such as looking up parameter annotations on interfaces.

See gh-29825
jhoeller added a commit that referenced this issue Jun 14, 2023
Avoiding cycle between web.method.support and web.method.annotation packages.

See gh-29825
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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants