Skip to content

Commit

Permalink
Prevent possible NPE when building violations report
Browse files Browse the repository at this point in the history
The NPE could happen when Response was used as a return type
and no @produces annotation was present.

Fixes: quarkusio/quarkus-workshops#183
  • Loading branch information
geoand authored and tmihalac committed Oct 27, 2022
1 parent 2980b51 commit 12f6d27
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
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

0 comments on commit 12f6d27

Please sign in to comment.