-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
ConstraintViolationExceptions thrown from REST endpoints not firing custom ExceptionMapper #36276
Comments
/cc @FroMage (resteasy-reactive), @stuartwdouglas (resteasy-reactive) |
So, I noticed that the @Valid annotation or even a manual throw of ConstraintViolation exception isn't firing my custom ExceptionMapper. I'm putting together a global exception handler which formats the incoming exceptions to nicely formatted JSON output based on the type of exception being converted. This works for other exceptions but it seems the I tried both the JAX-RS way of using the I put together a demo at: https://github.com/tmulle/quarkus-validation-test To test: *** Normal Exception ***
*** FORM encoded and Multipart Form ****
Response:
This is with Quarkus 3.4.2 and 3.4.1 This is the resource: @Path("/validate")
public class GreetingResource {
@Inject
Validator validator;
@POST
@Path("/form/manual")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public Response handleFormEncodedManual(HelloRequest request) {
Set<ConstraintViolation<HelloRequest>> errors = validator.validate(request);
if (!errors.isEmpty()) {
throw new ConstraintViolationException(errors);
}
return Response.ok("Hello " + request.getName()).build();
}
@POST
@Path("/form/auto")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public Response handleFormEncodedAuto(@Valid HelloRequest request) {
return Response.ok("Hello " + request.getName()).build();
}
@POST
@Path("/multipart/auto")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response handleMultiFormAuto(@Valid HelloRequest request) {
return Response.ok("Hello " + request.getName()).build();
}
@POST
@Path("/multipart/manual")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response handleMultiFormManual(HelloRequest request) {
Set<ConstraintViolation<HelloRequest>> errors = validator.validate(request);
if (!errors.isEmpty()) {
throw new ConstraintViolationException(errors);
}
return Response.ok("Hello " + request.getName()).build();
}
@GET
@Path("/boom")
@Produces(MediaType.TEXT_PLAIN)
public Response throwException() {
throw new RuntimeException("Boom!");
}
} This is the bean trying to validate: public class HelloRequest {
@RestForm
@Size(max=5, message = "Name cannot be greater than {max} characters")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} |
This original issue of I will post a ticket over there because the Sorry for all the changes, I went down the rabbit hole trying to find where the issue is and there seems to be 2 different issues. |
I think But even when defining the priority, I wonder if the most precise exception mapper won't be selected anyway. |
Correct
Correct again, that's what the JAX-RS / Jakarta REST spec mandates - more specific (in terms of the handled exception) exception mappers always take priority over more general ones |
Do you mind summorizing please? |
Duplicate of #41102 |
closing since this is related to #41102 |
Describe the bug
Not sure if this is by design or not, but when I have the following signature on my rest endpoint where I want to validate the fields in the incoming bean, the validation never happens automatically.
I have to manually call
validator.validate(formData)
using the injected Validator in my class.I didn't see anything in the docs saying I couldn't annotate a FormBean to use with the validation.
https://quarkus.io/guides/validation
The validation process doesn't happen automatically, uncommenting the validator code works and the validation happens.
Endpoint
Formbean
Expected behavior
Validation to automatically happen
Actual behavior
Have to manually trigger using the validator injection class
How to Reproduce?
No response
Output of
uname -a
orver
No response
Output of
java -version
No response
GraalVM version (if different from Java)
No response
Quarkus version or git rev
3.4.1
Build tool (ie. output of
mvnw --version
orgradlew --version
)No response
Additional information
No response
The text was updated successfully, but these errors were encountered: