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

Properly take produced media type into account when building validation error report #28343

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm. How did it even work? Shouldn't have it failed with a CNFE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two words: constant inlining :)

import org.jboss.resteasy.reactive.server.core.CurrentRequestManager;

@Provider
public class ResteasyReactiveViolationExceptionMapper implements ExceptionMapper<ValidationException> {

@Context
HttpHeaders headers;
private static final String VALIDATION_HEADER = "validation-exception";

@Override
public Response toResponse(ValidationException exception) {
Expand Down Expand Up @@ -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;
}
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);
}
}