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

Prevent possible NPE when building violations report #28462

Merged
merged 1 commit into from
Oct 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.common.util.ServerMediaType;
import org.jboss.resteasy.reactive.server.core.CurrentRequestManager;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;

@Provider
public class ResteasyReactiveViolationExceptionMapper implements ExceptionMapper<ValidationException> {
Expand Down Expand Up @@ -75,8 +77,7 @@ private Response buildViolationReportResponse(ConstraintViolationException cve)
// Check standard media types.
MediaType mediaType = ValidatorMediaTypeUtil.getAcceptMediaType(
rrContext.getHttpHeaders().getAcceptableMediaTypes(),
rrContext.getTarget() != null ? Arrays.asList(rrContext.getTarget().getProduces().getSortedMediaTypes())
: SUPPORTED_MEDIA_TYPES);
serverMediaTypes(rrContext));
if (mediaType == null) {
mediaType = MediaType.APPLICATION_JSON_TYPE;
}
Expand All @@ -91,4 +92,15 @@ private Response buildViolationReportResponse(ConstraintViolationException cve)
return builder.build();
}

private List<MediaType> serverMediaTypes(ResteasyReactiveRequestContext context) {
if (context.getTarget() == null) {
return SUPPORTED_MEDIA_TYPES;
}
ServerMediaType serverMediaType = context.getTarget().getProduces();
if (serverMediaType == null) {
return SUPPORTED_MEDIA_TYPES;
}
return Arrays.asList(serverMediaType.getSortedMediaTypes());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.hibernate.validator.constraints.Length;
import org.jboss.resteasy.reactive.RestPath;
Expand Down Expand Up @@ -88,6 +89,12 @@ public String testRestEndPointWithNoProduces(@Digits(integer = 5, fraction = 0)
return id;
}

@GET
@Path("/no-produces-with-response/{id}/")
public Response testRestEndPointWithNoProducesUsingResponse(@Digits(integer = 5, fraction = 0) @RestPath("id") String id) {
return Response.ok(id).build();
}

@GET
@Path("/rest-end-point-return-value-validation/{returnValue}/")
@Produces(MediaType.TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ public void testNoProduces() {
.body(containsString("numeric value out of bounds"));
}

@Test
public void testNoProducesWithResponse() {
RestAssured.given()
.get("/hibernate-validator/test/no-produces-with-response/plop/")
.then()
.statusCode(Response.Status.BAD_REQUEST.getStatusCode())
.body(containsString("numeric value out of bounds"));
}

@Test
public void testRestEndPointReturnValueValidation() {
// https://github.com/quarkusio/quarkus/issues/9174
Expand Down