From 6a0179ec861fe5095b335d5e8c64c3da4a554a3d Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 1 Jun 2023 12:20:11 +0300 Subject: [PATCH] When using RestMulti, don't require @Produces Fixes: #33746 --- .../test/headers/ResponseHeaderTest.java | 61 +++++++++++++++++++ .../handlers/PublisherResponseHandler.java | 12 +++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/headers/ResponseHeaderTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/headers/ResponseHeaderTest.java index 6b9774f5c1671..6c02c61cb12c1 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/headers/ResponseHeaderTest.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/headers/ResponseHeaderTest.java @@ -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; @@ -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; @@ -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 { @@ -275,6 +321,21 @@ public RestMulti getTestRestMulti3(@DefaultValue("foo") @RestQuery("h1") return RestMulti.fromUniResponse(getWrapper(header1, header2), Wrapper::getData, Wrapper::getHeaders); } + @GET + @Path("/rest-multi4") + public RestMulti 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 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); diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/handlers/PublisherResponseHandler.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/handlers/PublisherResponseHandler.java index 12c8f2722e74c..a6aa28fd1e632 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/handlers/PublisherResponseHandler.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/handlers/PublisherResponseHandler.java @@ -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 streamingResponseCustomizers = Collections.emptyList(); public void setStreamingResponseCustomizers(List streamingResponseCustomizers) { @@ -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) {