From 84e2402ca702b9eec5f8723b3b4779ebd856bd3b Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 11 Jun 2024 12:25:53 +0300 Subject: [PATCH] Take MediaType set in pre-match filter into when returning Response Fixes: #41097 (cherry picked from commit 57f38f152a6f8948d00f586bf5a07887d76a4020) --- .../serialization/DynamicEntityWriter.java | 11 +++++- .../matching/PreMatchAcceptInHeaderTest.java | 39 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/serialization/DynamicEntityWriter.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/serialization/DynamicEntityWriter.java index 452a083fce005..48b88ce0c0719 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/serialization/DynamicEntityWriter.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/serialization/DynamicEntityWriter.java @@ -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 accepts = context.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT); + for (String accept : accepts) { + negotiatedMediaType = context.getTarget().getProduces().negotiateProduces(accept).getKey(); + if (negotiatedMediaType != null) { + break; + } + } + List> writersList = serialisers.findWriters(null, entity.getClass(), negotiatedMediaType, RuntimeType.SERVER); if (!writersList.isEmpty()) { diff --git a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/matching/PreMatchAcceptInHeaderTest.java b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/matching/PreMatchAcceptInHeaderTest.java index 16b31cd54b127..e1c2f39b9caad 100644 --- a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/matching/PreMatchAcceptInHeaderTest.java +++ b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/matching/PreMatchAcceptInHeaderTest.java @@ -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; @@ -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 { @@ -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) {