Skip to content

Commit

Permalink
When using RestMulti, don't require @produces
Browse files Browse the repository at this point in the history
Fixes: #33746
  • Loading branch information
geoand committed Jun 1, 2023
1 parent 86fabfc commit 6a0179e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.ResponseHeader;
import org.jboss.resteasy.reactive.ResponseStatus;
Expand All @@ -23,6 +24,7 @@

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.http.Headers;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
Expand Down Expand Up @@ -179,6 +181,50 @@ public void testReturnRestMulti3() {
"header2", "h2"));
}

@Test
public void testReturnRestMulti4() {
RestAssured
.given()
.get("/test/rest-multi2")
.then()
.statusCode(200)
.contentType(ContentType.TEXT)
.headers(Map.of(
"Access-Control-Allow-Origin", "foo",
"Keep-Alive", "bar"));

RestAssured
.given()
.get("/test/rest-multi2?keepAlive=dummy")
.then()
.statusCode(200)
.contentType(ContentType.TEXT)
.headers(Map.of(
"Access-Control-Allow-Origin", "foo",
"Keep-Alive", "dummy"));
}

@Test
public void testReturnRestMulti5() {
RestAssured
.given()
.get("/test/rest-multi3")
.then()
.statusCode(200)
.headers(Map.of(
"header1", "foo",
"header2", "bar"));

RestAssured
.given()
.get("/test/rest-multi3?h1=h1&h2=h2")
.then()
.statusCode(200)
.headers(Map.of(
"header1", "h1",
"header2", "h2"));
}

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

Expand Down Expand Up @@ -275,6 +321,21 @@ public RestMulti<byte[]> getTestRestMulti3(@DefaultValue("foo") @RestQuery("h1")
return RestMulti.fromUniResponse(getWrapper(header1, header2), Wrapper::getData, Wrapper::getHeaders);
}

@GET
@Path("/rest-multi4")
public RestMulti<byte[]> getTestRestMulti4(@DefaultValue("bar") @RestQuery String keepAlive) {
return RestMulti.fromMultiData(Multi.createFrom().item("test".getBytes(StandardCharsets.UTF_8)))
.header("Access-Control-Allow-Origin", "foo")
.header("Keep-Alive", keepAlive).header("Content-Type", MediaType.TEXT_PLAIN).build();
}

@GET
@Path("/rest-multi5")
public RestMulti<byte[]> getTestRestMulti5(@DefaultValue("foo") @RestQuery("h1") String header1,
@DefaultValue("bar") @RestQuery("h2") String header2) {
return RestMulti.fromUniResponse(getWrapper(header1, header2), Wrapper::getData, Wrapper::getHeaders);
}

private IllegalArgumentException createException() {
IllegalArgumentException result = new IllegalArgumentException();
result.setStackTrace(EMPTY_STACK_TRACE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class PublisherResponseHandler implements ServerRestHandler {

private static final String JSON = "json";

private static final ServerMediaType REST_MULTI_DEFAULT_SERVER_MEDIA_TYPE = new ServerMediaType(
List.of(MediaType.APPLICATION_OCTET_STREAM_TYPE), StandardCharsets.UTF_8.name(), false);

private List<StreamingResponseCustomizer> streamingResponseCustomizers = Collections.emptyList();

public void setStreamingResponseCustomizers(List<StreamingResponseCustomizer> streamingResponseCustomizers) {
Expand Down Expand Up @@ -301,8 +304,13 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
// or make this SSE produce build-time
ServerMediaType produces = requestContext.getTarget().getProduces();
if (produces == null) {
throw new IllegalStateException(
"Negotiation or dynamic media type not supported yet for Multi: please use the @Produces annotation when returning a Multi");
if (result instanceof RestMulti) {
produces = REST_MULTI_DEFAULT_SERVER_MEDIA_TYPE;
} else {
throw new IllegalStateException(
"Negotiation or dynamic media type not supported yet for Multi: please use the @Produces annotation when returning a Multi");
}

}
MediaType[] mediaTypes = produces.getSortedOriginalMediaTypes();
if (mediaTypes.length != 1) {
Expand Down

0 comments on commit 6a0179e

Please sign in to comment.