Skip to content

Commit e07acf1

Browse files
committed
Take MediaType set in pre-match filter into when returning Response
Fixes: quarkusio#41097
1 parent c296521 commit e07acf1

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/serialization/DynamicEntityWriter.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ public void write(ResteasyReactiveRequestContext context, Object entity) throws
4141
ServerHttpRequest vertxRequest = context.serverRequest();
4242
// first check and see if the resource method defined a media type and try to use it
4343
if ((context.getTarget() != null) && (context.getTarget().getProduces() != null)) {
44-
MediaType negotiatedMediaType = context.getTarget().getProduces()
45-
.negotiateProduces(vertxRequest.getRequestHeader(HttpHeaders.ACCEPT)).getKey();
44+
MediaType negotiatedMediaType = null;
45+
List<String> accepts = context.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT);
46+
for (String accept : accepts) {
47+
negotiatedMediaType = context.getTarget().getProduces().negotiateProduces(accept).getKey();
48+
if (negotiatedMediaType != null) {
49+
break;
50+
}
51+
}
52+
4653
List<MessageBodyWriter<?>> writersList = serialisers.findWriters(null, entity.getClass(), negotiatedMediaType,
4754
RuntimeType.SERVER);
4855
if (!writersList.isEmpty()) {

independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/matching/PreMatchAcceptInHeaderTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import jakarta.ws.rs.core.HttpHeaders;
2121
import jakarta.ws.rs.core.MediaType;
2222
import jakarta.ws.rs.core.MultivaluedMap;
23+
import jakarta.ws.rs.core.Response;
2324
import jakarta.ws.rs.ext.Provider;
2425

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

127+
@Test
128+
void responseEntityJsonWithoutAcceptToTextInFilter() {
129+
given().accept("application/json")
130+
.when()
131+
.get("test/response")
132+
.then()
133+
.statusCode(200)
134+
.body(containsString("\"text\""));
135+
}
136+
137+
@Test
138+
void responseEntityTextWithoutAcceptToTextInFilter() {
139+
given().accept("text/plain")
140+
.when()
141+
.get("test/response")
142+
.then()
143+
.statusCode(200)
144+
.body(equalTo("text"));
145+
}
146+
147+
@Test
148+
void responseEntityTextWithAcceptToTextInFilter() {
149+
given().accept("application/json")
150+
.header("x-set-accept-to-text", "true")
151+
.when()
152+
.get("test/response")
153+
.then()
154+
.statusCode(200)
155+
.body(equalTo("text"));
156+
}
157+
126158
@Path("/test")
127159
public static class Resource {
128160

@@ -152,6 +184,13 @@ public String html() {
152184
public Entity entity() {
153185
return new Entity("text");
154186
}
187+
188+
@GET
189+
@Path("response")
190+
@Produces({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML })
191+
public Response response() {
192+
return Response.ok(new Entity("text")).build();
193+
}
155194
}
156195

157196
public record Entity(String value) {

0 commit comments

Comments
 (0)