From c6b03f9ad8b6b03df368373d91828cb20a05ea3f Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Fri, 2 Aug 2024 10:59:18 +0300 Subject: [PATCH] Don't convert WebApplicationException in REST Client when produced by a custom mapper The idea is that if a user has opted to create an exception, we should always honor it - even if it's a WebApplicationException Fixes: #42275 --- .../test/BadRequestNotPropagatedTestCase.java | 32 +++++++++++++++++-- .../MicroProfileRestClientResponseFilter.java | 3 ++ .../client/impl/RestClientRequestContext.java | 18 ++++++++--- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/extensions/resteasy-reactive/rest-client-jackson/deployment/src/test/java/io/quarkus/rest/client/reactive/jackson/test/BadRequestNotPropagatedTestCase.java b/extensions/resteasy-reactive/rest-client-jackson/deployment/src/test/java/io/quarkus/rest/client/reactive/jackson/test/BadRequestNotPropagatedTestCase.java index 9e03af7e052cb..f4ee1e287ce3d 100644 --- a/extensions/resteasy-reactive/rest-client-jackson/deployment/src/test/java/io/quarkus/rest/client/reactive/jackson/test/BadRequestNotPropagatedTestCase.java +++ b/extensions/resteasy-reactive/rest-client-jackson/deployment/src/test/java/io/quarkus/rest/client/reactive/jackson/test/BadRequestNotPropagatedTestCase.java @@ -3,7 +3,6 @@ import java.net.URL; import java.util.List; -import jakarta.inject.Inject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -22,6 +21,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.rest.client.reactive.ClientExceptionMapper; import io.quarkus.test.QuarkusUnitTest; import io.quarkus.test.common.http.TestHTTPResource; @@ -55,6 +55,12 @@ public void testBadRequest() { Assertions.assertEquals(500, data.getStatus()); } + @Test + public void testBadRequestWithCusterMapper() { + Response data = client.target(url.toExternalForm() + "/bad-server/with-custom-mapper").request().get(); + Assertions.assertEquals(999, data.getStatus()); + } + @Path("/bad") public static class Bad { @@ -75,6 +81,19 @@ public interface BadClient { JsonObject get(String json); } + @Path("/bad") + @RegisterRestClient(baseUri = "http://localhost:8081") + public interface BadClientWithCustomMapper extends BadClient { + + @ClientExceptionMapper + static RuntimeException toException(Response response) { + if (response.getStatus() == 400) { + return new WebApplicationException(999); + } + return null; + } + } + static class JsonObject { String name; } @@ -82,10 +101,12 @@ static class JsonObject { @Path("/bad-server") public static class BadServer { - @Inject @RestClient BadClient badClient; + @RestClient + BadClientWithCustomMapper badClientWithCustomMapper; + @GET public JsonObject get() { try { @@ -104,5 +125,12 @@ public JsonObject get() { return null; } } + + @GET + @Path("with-custom-mapper") + public JsonObject getWithCustomMapper() { + return badClientWithCustomMapper.get("{name:foo}"); + } + } } diff --git a/extensions/resteasy-reactive/rest-client/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/MicroProfileRestClientResponseFilter.java b/extensions/resteasy-reactive/rest-client/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/MicroProfileRestClientResponseFilter.java index 1d369c71e5a47..5ddb91b73116b 100644 --- a/extensions/resteasy-reactive/rest-client/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/MicroProfileRestClientResponseFilter.java +++ b/extensions/resteasy-reactive/rest-client/runtime/src/main/java/io/quarkus/rest/client/reactive/runtime/MicroProfileRestClientResponseFilter.java @@ -1,5 +1,7 @@ package io.quarkus.rest.client.reactive.runtime; +import static org.jboss.resteasy.reactive.client.impl.RestClientRequestContext.INVOKED_EXCEPTION_MAPPER_CLASS_NAME_PROP; + import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -51,6 +53,7 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re } else { throwable = exceptionMapper.toThrowable(response); } + requestContext.setProperty(INVOKED_EXCEPTION_MAPPER_CLASS_NAME_PROP, exceptionMapper.getClass().getName()); if (throwable != null) { throw new UnwrappableException(throwable); } diff --git a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/RestClientRequestContext.java b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/RestClientRequestContext.java index 9ce0103471997..89002b184d949 100644 --- a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/RestClientRequestContext.java +++ b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/RestClientRequestContext.java @@ -58,11 +58,14 @@ public class RestClientRequestContext extends AbstractResteasyReactiveContext