diff --git a/extensions/hibernate-validator/runtime/pom.xml b/extensions/hibernate-validator/runtime/pom.xml index 2ecab2bd809c7..df18cf60093ef 100644 --- a/extensions/hibernate-validator/runtime/pom.xml +++ b/extensions/hibernate-validator/runtime/pom.xml @@ -82,6 +82,11 @@ + + io.quarkus.resteasy.reactive + resteasy-reactive + true + org.jboss.spec.javax.ws.rs jboss-jaxrs-api_2.1_spec diff --git a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveViolationExceptionMapper.java b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveViolationExceptionMapper.java index ec3f4912b1f69..7e55a79e80a09 100644 --- a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveViolationExceptionMapper.java +++ b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveViolationExceptionMapper.java @@ -1,6 +1,9 @@ package io.quarkus.hibernate.validator.runtime.jaxrs; +import static io.quarkus.hibernate.validator.runtime.jaxrs.ValidatorMediaTypeUtil.SUPPORTED_MEDIA_TYPES; + import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Set; @@ -10,21 +13,18 @@ import javax.validation.ElementKind; import javax.validation.Path; import javax.validation.ValidationException; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; -import org.jboss.resteasy.api.validation.Validation; +import org.jboss.resteasy.reactive.server.core.CurrentRequestManager; @Provider public class ResteasyReactiveViolationExceptionMapper implements ExceptionMapper { - @Context - HttpHeaders headers; + private static final String VALIDATION_HEADER = "validation-exception"; @Override public Response toResponse(ValidationException exception) { @@ -69,10 +69,14 @@ private boolean isReturnValueViolation(ConstraintViolation violation) { private Response buildViolationReportResponse(ConstraintViolationException cve) { Status status = Status.BAD_REQUEST; Response.ResponseBuilder builder = Response.status(status); - builder.header(Validation.VALIDATION_HEADER, "true"); + builder.header(VALIDATION_HEADER, "true"); + var rrContext = CurrentRequestManager.get(); // Check standard media types. - MediaType mediaType = ValidatorMediaTypeUtil.getAcceptMediaTypeFromSupported(headers.getAcceptableMediaTypes()); + MediaType mediaType = ValidatorMediaTypeUtil.getAcceptMediaType( + rrContext.getHttpHeaders().getAcceptableMediaTypes(), + rrContext.getTarget() != null ? Arrays.asList(rrContext.getTarget().getProduces().getSortedMediaTypes()) + : SUPPORTED_MEDIA_TYPES); if (mediaType == null) { mediaType = MediaType.APPLICATION_JSON_TYPE; } diff --git a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ValidatorMediaTypeUtil.java b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ValidatorMediaTypeUtil.java index 8eeede84b2166..90e5ac4f82323 100644 --- a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ValidatorMediaTypeUtil.java +++ b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ValidatorMediaTypeUtil.java @@ -10,7 +10,7 @@ */ public final class ValidatorMediaTypeUtil { - private static final List SUPPORTED_MEDIA_TYPES = Arrays.asList( + static final List SUPPORTED_MEDIA_TYPES = Arrays.asList( MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE, MediaType.TEXT_XML_TYPE, @@ -20,16 +20,6 @@ private ValidatorMediaTypeUtil() { } - /** - * Look up the right media type taking into account the HTTP request and the supported media types. - * - * @param mediaTypesFromRequest list of media types in the HTTP request. - * @return one supported media type from either the HTTP request or the annotation. - */ - public static MediaType getAcceptMediaTypeFromSupported(List mediaTypesFromRequest) { - return getAcceptMediaType(mediaTypesFromRequest, SUPPORTED_MEDIA_TYPES); - } - /** * Look up the right media type taking into account the HTTP request and the media types defined in the `@Produces` * annotation. diff --git a/integration-tests/hibernate-validator-resteasy-reactive/src/main/java/io/quarkus/it/hibernate/validator/TextResource.java b/integration-tests/hibernate-validator-resteasy-reactive/src/main/java/io/quarkus/it/hibernate/validator/TextResource.java new file mode 100644 index 0000000000000..9d7a173286183 --- /dev/null +++ b/integration-tests/hibernate-validator-resteasy-reactive/src/main/java/io/quarkus/it/hibernate/validator/TextResource.java @@ -0,0 +1,27 @@ +package io.quarkus.it.hibernate.validator; + +import javax.validation.constraints.Digits; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("text") +public class TextResource { + + @GET + @Path("/validate/{id}") + public String validate( + @Digits(integer = 5, fraction = 0, message = "numeric value out of bounds") @PathParam("id") String id) { + return id; + } + + @GET + @Path("/validate/text/{id}") + @Produces(MediaType.TEXT_PLAIN) + public String validateText( + @Digits(integer = 5, fraction = 0, message = "numeric value out of bounds") @PathParam("id") String id) { + return id; + } +} diff --git a/integration-tests/hibernate-validator-resteasy-reactive/src/test/java/io/quarkus/it/hibernate/validator/TextResourceTest.java b/integration-tests/hibernate-validator-resteasy-reactive/src/test/java/io/quarkus/it/hibernate/validator/TextResourceTest.java new file mode 100644 index 0000000000000..b7fc426b18367 --- /dev/null +++ b/integration-tests/hibernate-validator-resteasy-reactive/src/test/java/io/quarkus/it/hibernate/validator/TextResourceTest.java @@ -0,0 +1,28 @@ +package io.quarkus.it.hibernate.validator; + +import static io.restassured.RestAssured.when; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; + +@QuarkusTest +public class TextResourceTest { + + @Test + public void fetchDefault() { + when().get("/text/validate/boom") + .then() + .statusCode(400) + .contentType(ContentType.TEXT); + } + + @Test + public void fetchText() { + when().get("/text/validate/boom") + .then() + .statusCode(400) + .contentType(ContentType.TEXT); + } +}