Skip to content

Commit

Permalink
Use proper media type when executing writer interceptors
Browse files Browse the repository at this point in the history
The one from the response can be null if we return a partial response
but the type is defined by @produces.
We need to consider the one from the context.

Fixes #34839
  • Loading branch information
gsmet committed Oct 31, 2024
1 parent 786aa92 commit a60aca2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.resteasy.reactive.server.test.providers;

import java.io.IOException;

import jakarta.annotation.Priority;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;
import jakarta.ws.rs.ext.WriterInterceptorContext;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class WriterInterceptorContentTypeOverrideTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot(
jar -> jar.addClasses(MediaTypeResponseInterceptor.class, TestResource.class));

@Test
public void producesMediaTypePresentInWriterInterceptor() {
RestAssured.when().get("/test").then().body(Matchers.containsString("Hello"));
}

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

@GET
@Produces(MediaType.TEXT_XML)
public Response hello() {
Greeting greeting = new Greeting("Hello");
return Response.ok(greeting).type(MediaType.TEXT_PLAIN).build();
}
}

public record Greeting(String message) {
}

@Priority(5000)
@Provider
public static class MediaTypeResponseInterceptor implements jakarta.ws.rs.ext.WriterInterceptor {

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
if (!context.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) {
throw new IllegalStateException("MediaType was not overridden by Response, got: " + context.getMediaType()
+ " instead of expected: " + MediaType.TEXT_PLAIN);
}
context.proceed();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.resteasy.reactive.server.test.providers;

import java.io.IOException;

import jakarta.annotation.Priority;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;
import jakarta.ws.rs.ext.WriterInterceptorContext;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class WriterInterceptorContentTypeTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot(
jar -> jar.addClasses(MediaTypeResponseInterceptor.class, TestResource.class));

@Test
public void producesMediaTypePresentInWriterInterceptor() {
RestAssured.when().get("/test").then().body(Matchers.containsString("Hello"));
}

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

@GET
@Produces(MediaType.TEXT_XML)
public Response hello() {
Greeting greeting = new Greeting("Hello");
return Response.ok(greeting).build();
}
}

public record Greeting(String message) {
}

@Priority(5000)
@Provider
public static class MediaTypeResponseInterceptor implements jakarta.ws.rs.ext.WriterInterceptor {

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
if (!context.getMediaType().isCompatible(MediaType.TEXT_XML_TYPE)) {
throw new IllegalStateException("MediaType was not provided, got: " + context.getMediaType()
+ " instead of expected: " + MediaType.TEXT_XML);
}
context.proceed();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ public static boolean invokeWriter(ResteasyReactiveRequestContext context, Objec
public static void runWriterInterceptors(ResteasyReactiveRequestContext context, Object entity, MessageBodyWriter writer,
Response response, WriterInterceptor[] writerInterceptor, ServerSerialisers serialisers) throws IOException {
WriterInterceptorContextImpl wc = new WriterInterceptorContextImpl(context, writerInterceptor, writer,
context.getAllAnnotations(), entity.getClass(), context.getGenericReturnType(), entity, response.getMediaType(),
response.getHeaders(), serialisers);
context.getAllAnnotations(), entity.getClass(), context.getGenericReturnType(), entity,
context.getResponseContentType().getMediaType(), response.getHeaders(), serialisers);
wc.proceed();
}

Expand Down

0 comments on commit a60aca2

Please sign in to comment.