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

Take MediaType set in pre-match filter into when returning Response #41111

Merged
merged 1 commit into from
Jun 11, 2024
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 @@ -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
Loading