Skip to content

Commit

Permalink
Properly take produced media type into account when building validati…
Browse files Browse the repository at this point in the history
…on error report

Fixes: quarkusio#28324
  • Loading branch information
geoand authored and igorregis committed Oct 17, 2022
1 parent 183e633 commit 010d300
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
5 changes: 5 additions & 0 deletions extensions/hibernate-validator/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus.resteasy.reactive</groupId>
<artifactId>resteasy-reactive</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,22 +13,19 @@
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.reactive.server.core.CurrentRequestManager;

@Provider
public class ResteasyReactiveViolationExceptionMapper implements ExceptionMapper<ValidationException> {

private static final String VALIDATION_HEADER = "validation-exception";

@Context
HttpHeaders headers;

@Override
public Response toResponse(ValidationException exception) {
if (!(exception instanceof ResteasyReactiveViolationException)) {
Expand Down Expand Up @@ -71,8 +71,12 @@ private Response buildViolationReportResponse(ConstraintViolationException cve)
Response.ResponseBuilder builder = Response.status(status);
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public final class ValidatorMediaTypeUtil {

private static final List<MediaType> SUPPORTED_MEDIA_TYPES = Arrays.asList(
static final List<MediaType> SUPPORTED_MEDIA_TYPES = Arrays.asList(
MediaType.APPLICATION_JSON_TYPE,
MediaType.APPLICATION_XML_TYPE,
MediaType.TEXT_XML_TYPE,
Expand All @@ -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<MediaType> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 010d300

Please sign in to comment.