Skip to content

Commit

Permalink
Properly pass media type to MessageBodyWriters
Browse files Browse the repository at this point in the history
Fixes: #25263
(cherry picked from commit de4b81a)
  • Loading branch information
geoand authored and gsmet committed May 3, 2022
1 parent 29bac1d commit 75b74fe
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public static boolean invokeWriter(ResteasyReactiveRequestContext context, Objec
}
if (writerInterceptors == null) {
writer.writeTo(entity, entity.getClass(), context.getGenericReturnType(),
context.getAllAnnotations(), response.getMediaType(), response.getHeaders(),
context.getAllAnnotations(), context.getResponseMediaType(), response.getHeaders(),
context.getOrCreateOutputStream());
context.getOrCreateOutputStream().close();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public DynamicEntityWriter(ServerSerialisers serialisers) {
public void write(ResteasyReactiveRequestContext context, Object entity) throws IOException {
EncodedMediaType producesMediaType = context.getResponseContentType();
MessageBodyWriter<?>[] writers = null;
MediaType serverSerializersMediaType = null;
if (producesMediaType == null) {
MediaType selectedMediaType = null;
boolean mediaTypeComesFromClient = false;
Expand Down Expand Up @@ -87,6 +88,7 @@ public void write(ResteasyReactiveRequestContext context, Object entity) throws
httpServerResponse.end();
return;
} else {
serverSerializersMediaType = selectedMediaType;
context.setResponseContentType(selectedMediaType);
// this will be used as the fallback if Response does NOT contain a type
context.serverResponse().addResponseHeader(HttpHeaders.CONTENT_TYPE, selectedMediaType.toString());
Expand All @@ -99,7 +101,7 @@ public void write(ResteasyReactiveRequestContext context, Object entity) throws
.toArray(ServerSerialisers.NO_WRITER);
}
for (MessageBodyWriter<?> w : writers) {
if (ServerSerialisers.invokeWriter(context, entity, w, serialisers)) {
if (ServerSerialisers.invokeWriter(context, entity, w, serialisers, serverSerializersMediaType)) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.jboss.resteasy.reactive.server.vertx.test.mediatype;

import static io.restassured.RestAssured.given;
import static org.hamcrest.core.StringStartsWith.startsWith;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.function.Supplier;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class MessageBodyWriteTest {

@RegisterExtension
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(InvalidContentTypeTest.HelloResource.class);
}
});

@Test
public void test() {
given().when().get("/test").then()
.statusCode(200)
.contentType(MediaType.APPLICATION_JSON)
.header("Content-Type-Copy", startsWith(MediaType.APPLICATION_JSON));
}

@Path("test")
public static class TestResource {

@GET
public Response response() {
return Response.ok(Map.of("key", "value")).build();
}
}

@Provider
@Produces(MediaType.APPLICATION_JSON)
public static class GenericJSONSerializer implements MessageBodyWriter<Object> {
@Override
public long getSize(final Object value, final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
return -1;
}

@Override
public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType) {
return true;
}

@Override
public void writeTo(final Object value, final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
throws IOException {

httpHeaders.add("Content-Type-Copy", mediaType.toString());
entityStream.write("{\"foo\": \"bar\"}".getBytes(StandardCharsets.UTF_8));
}
}
}

0 comments on commit 75b74fe

Please sign in to comment.