Skip to content

Commit

Permalink
Take MediaType set in pre-match filter into when returning Response
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jun 11, 2024
1 parent 99d63a7 commit 57f38f1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ public void write(ResteasyReactiveRequestContext context, Object entity) throws
ServerHttpRequest vertxRequest = context.serverRequest();
// first check and see if the resource method defined a media type and try to use it
if ((context.getTarget() != null) && (context.getTarget().getProduces() != null)) {
MediaType negotiatedMediaType = context.getTarget().getProduces()
.negotiateProduces(vertxRequest.getRequestHeader(HttpHeaders.ACCEPT)).getKey();
MediaType negotiatedMediaType = null;
List<String> accepts = context.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT);
for (String accept : accepts) {
negotiatedMediaType = context.getTarget().getProduces().negotiateProduces(accept).getKey();
if (negotiatedMediaType != null) {
break;
}
}

List<MessageBodyWriter<?>> writersList = serialisers.findWriters(null, entity.getClass(), negotiatedMediaType,
RuntimeType.SERVER);
if (!writersList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;

import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
Expand Down Expand Up @@ -123,6 +124,37 @@ void entityTextWithAcceptToTextInFilter() {
.body(equalTo("text"));
}

@Test
void responseEntityJsonWithoutAcceptToTextInFilter() {
given().accept("application/json")
.when()
.get("test/response")
.then()
.statusCode(200)
.body(containsString("\"text\""));
}

@Test
void responseEntityTextWithoutAcceptToTextInFilter() {
given().accept("text/plain")
.when()
.get("test/response")
.then()
.statusCode(200)
.body(equalTo("text"));
}

@Test
void responseEntityTextWithAcceptToTextInFilter() {
given().accept("application/json")
.header("x-set-accept-to-text", "true")
.when()
.get("test/response")
.then()
.statusCode(200)
.body(equalTo("text"));
}

@Path("/test")
public static class Resource {

Expand Down Expand Up @@ -152,6 +184,13 @@ public String html() {
public Entity entity() {
return new Entity("text");
}

@GET
@Path("response")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public Response response() {
return Response.ok(new Entity("text")).build();
}
}

public record Entity(String value) {
Expand Down

0 comments on commit 57f38f1

Please sign in to comment.