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

HandlerMethodValidationException.Visitor should support RequestBody with method parameter constraints #34549

Closed
jarzynp opened this issue Mar 6, 2025 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@jarzynp
Copy link

jarzynp commented Mar 6, 2025

Environment

Spring Boot: 3.4.3
Spring Web: 6.2.3
Java: 17


Expected Behavior

When a @RequestBody value is invalid, HandlerMethodValidationException.Visitor should call the appropriate method.

Observed Behavior

Instead, HandlerMethodValidationException.Visitor throws IllegalStateException with "Expected ParameterErrors" message.

Example code

@Target({ElementType.TYPE_USE, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@NotEmpty
@Pattern(regexp = "^[a-z0-9]{1,10}$", message = ResId.RULES)
public @interface ResId {

    String RULES = "Identifier doesn't follow defined rules.";

    String message() default RULES;

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}
@RestController
@RequestMapping("/api")
public class Controller {

    @PostMapping("/{id}/modify")
    public ResponseEntity<Object> modify(
        @PathVariable @ResId final String id,
        @RequestBody @Valid final List<@ResId String> linkedResourceIds) {
        return ResponseEntity.noContent();
    }

}

Example request

HTTP Method = POST
Request URI = /api/foo/modify
Body = ["aasfw44t23gsdge4g3wcsdf43"]

Insights

In HandlerMethodValidationException#visitResults(HandlerMethodValidationException.Visitor):

RequestBody requestBody = param.getParameterAnnotation(RequestBody.class);
if (requestBody != null) {
	visitor.requestBody(requestBody, asErrors(result));
	continue;
}

the condition if (requestBody != null) is true, but the asErrors(result) is throwing the exception because in this case result is not an instance of ParameterErrors but ParameterValidationResult.

Image
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 6, 2025
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Mar 6, 2025
@rstoyanchev rstoyanchev self-assigned this Mar 6, 2025
@rstoyanchev rstoyanchev changed the title HandlerMethodValidationException.Visitor throws IllegalStateException when the list of objects as a request body is invalid HandlerMethodValidationException.Visitor throws IllegalStateException for ParameterValidationResult from RequestBody validation Mar 6, 2025
@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 Mar 6, 2025
@rstoyanchev rstoyanchev added this to the 6.2.4 milestone Mar 6, 2025
@rstoyanchev rstoyanchev changed the title HandlerMethodValidationException.Visitor throws IllegalStateException for ParameterValidationResult from RequestBody validation HandlerMethodValidationException.Visitor should support RequestBody with method parameter constraints Mar 10, 2025
rstoyanchev added a commit that referenced this issue Mar 10, 2025
rstoyanchev added a commit that referenced this issue Mar 10, 2025
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

4 participants