Closed
Description
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
.
