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 handling of method validation errors #30644

Closed
Tracked by #30645
rstoyanchev opened this issue Jun 12, 2023 · 2 comments
Closed
Tracked by #30645

Spring MVC and WebFlux handling of method validation errors #30644

rstoyanchev opened this issue Jun 12, 2023 · 2 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 Jun 12, 2023

The new MethodValidationException introduced as part of #29825 needs to have default handling in Spring MVC and WebFlux, including RFC 7807 support, similar to the handling of BindException but reflecting errors for multiple method parameters / or return value, represented with ParameterValidationResult and ParameterErrors.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Jun 12, 2023
@rstoyanchev rstoyanchev added this to the 6.1.0-M2 milestone Jun 12, 2023
@rstoyanchev rstoyanchev self-assigned this Jun 12, 2023
rstoyanchev added a commit that referenced this issue Jul 3, 2023
Remove throwIfViolationsPresent and replace with static factory
methods on MethodValidationException taking MethodValidationResult,
which makes handling more explicit and allows choice of what
exception to raise.

Update MethodValidationResult to expose the target, the method, and
forReturnValue flag, so the code handling an exception will have
access to all details.

See gh-30644
rstoyanchev added a commit that referenced this issue Jul 3, 2023
- Update method order
- Do not automatically create MessageSource arguments in
WebExchangeBindException constructor as they're more likely to be
created via getDetailMessageArguments with MessageSource passed in.

See gh-30644
rstoyanchev added a commit that referenced this issue Jul 3, 2023
This deprecates static methods in MethodArgumentNotValidException
which is not a great vehicle for such methods.

See gh-30644
rstoyanchev added a commit that referenced this issue Jul 3, 2023
To handle method validation errors in ResponseEntityExceptionHandler,
MethodValidationException and associated types should not depend on
Bean Validation. To that effect:

1. MethodValidationResult and ParameterValidationResult no longer make
the underlying ConstraintViolation set available, and instead expose
only the adapted validation errors (MessageSourceResolvable, Errors),
analogous to what SpringValidatorAdapter does. And likewise
MethodValidationException no longer extends ConstraintViolationException.

2. MethodValidationPostProcessor has a new property
adaptConstraintViolations to decide whether to simply raise
ConstraintViolationException, or otherwise to adapt the ConstraintViolations
and raise MethodValidationException instead, with the former is the default
for compatibility.

3. As a result, the MethodValidator contract can now expose methods that
return MethodValidationResult, which provided more flexibility for handling,
and it allows MethodValidationAdapter to implement MethodValidator directly.

4. Update Javadoc in method validation classes to reflect this shift, and
use terminology consistent with Spring validation in classes without an
explicit dependency on Bean Validation.

See gh-30644
rstoyanchev added a commit that referenced this issue Jul 3, 2023
Extract the default logic for resolving the name of an @Valid
parameter into an ObjectNameResolver, and use it when there isn't
one configured.

See gh-30644
rstoyanchev added a commit that referenced this issue Jul 3, 2023
Extract classes from ~.validation.beanvalidation without a direct
dependency on beanvalidation.

See gh-30644
@rstoyanchev rstoyanchev changed the title Support handling of MethodValidationException Support handling of method validation errors Jul 4, 2023
@rstoyanchev rstoyanchev changed the title Support handling of method validation errors Spring MVC and WebFlux handling of method validation errors Jul 4, 2023
@rstoyanchev
Copy link
Contributor Author

rstoyanchev commented Jul 4, 2023

There is now a new HandlerMethodValidationException, similar to the MethodValidationException raised when method validation is applied via AOP (to any Spring component), but extending from ResponseStatusException and thrown from the built-in method validation support for @RequestMapping methods.

The new exception is handled in ResponseEntityExceptionHandler and in DefaultHandlerExceptionResolver
as e 400 error if is for method arguments, and as a 500 error if it is for a return value. MethodValidationException (possibly from an underlying service) is handled as a 500 error.

By default for RFC 7807, the detail of ProblemDetail is just a short message "Validation failure". You can customize that with a MessageSource as described in the reference documentation where the {0} argument for HandlerMethodValidationException is the list of all errors. For more control, handle this exception directly and provide it with a Visitor to handle controller method parameters individually.

@ctapobep
Copy link

From the documentation it's not clear - does this mean that the old MethodArgumentNotValidException isn't going to be used by Spring MVC anymore? We should always expect HandlerMethodValidationException on the API level from now on?

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