From eb1ba439d9e9312e50774175eb704c4217a709e5 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 27 Feb 2024 13:32:15 +0200 Subject: [PATCH] Make Sub Resources unremovable beans Closes: #5314 --- .../ResteasyReactiveCDIProcessor.java | 22 ++++++++++++++----- .../server/test/SubResourcesAsBeansTest.java | 5 +++-- .../test/devmode/SubResourceDevModeTest.java | 5 ++++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveCDIProcessor.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveCDIProcessor.java index a9c80ff5f2398..74a5ed3c26677 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveCDIProcessor.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveCDIProcessor.java @@ -88,16 +88,26 @@ void unremovableContextMethodParams(Optional re @BuildStep void subResourcesAsBeans(ResourceScanningResultBuildItem setupEndpointsResult, List subResourcesAsBeans, - BuildProducer producer) { - if (subResourcesAsBeans.isEmpty() || setupEndpointsResult.getResult().getPossibleSubResources().isEmpty()) { + BuildProducer unremovableProducer, + BuildProducer additionalProducer) { + Map possibleSubResources = setupEndpointsResult.getResult().getPossibleSubResources(); + if (possibleSubResources.isEmpty()) { return; } - List classNames = new ArrayList<>(setupEndpointsResult.getResult().getPossibleSubResources().size()); - for (DotName subResourceClass : setupEndpointsResult.getResult().getPossibleSubResources().keySet()) { - classNames.add(subResourceClass.toString()); + // make SubResources unremovable - this will only apply if they become beans by some other means + unremovableProducer.produce(UnremovableBeanBuildItem.beanTypes(possibleSubResources.keySet())); + + if (subResourcesAsBeans.isEmpty()) { + return; + } + + // now actually make SubResources beans as it was requested via build item + AdditionalBeanBuildItem.Builder builder = AdditionalBeanBuildItem.builder(); + for (DotName subResourceClass : possibleSubResources.keySet()) { + builder.addBeanClass(subResourceClass.toString()); } - producer.produce(new AdditionalBeanBuildItem(classNames.toArray(new String[0]))); + additionalProducer.produce(builder.build()); } // when an interface is annotated with @Path and there is only one implementation of it that is not annotated with @Path, diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/SubResourcesAsBeansTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/SubResourcesAsBeansTest.java index 20767bbb3c741..9be0b0abbea24 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/SubResourcesAsBeansTest.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/SubResourcesAsBeansTest.java @@ -7,6 +7,7 @@ import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; +import jakarta.ws.rs.container.ResourceContext; import jakarta.ws.rs.core.HttpHeaders; import org.hamcrest.Matchers; @@ -63,11 +64,11 @@ public MiddleRestResource hello(String first) { public static class MiddleRestResource { @Inject - RestSubResource restSubResource; + ResourceContext resourceContext; @Path("{last}") public RestSubResource hello() { - return restSubResource; + return resourceContext.getResource(RestSubResource.class); } } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/devmode/SubResourceDevModeTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/devmode/SubResourceDevModeTest.java index b335d931fd040..3431f27270c61 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/devmode/SubResourceDevModeTest.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/devmode/SubResourceDevModeTest.java @@ -4,6 +4,7 @@ import java.util.function.Supplier; +import jakarta.inject.Singleton; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -50,10 +51,11 @@ public static class Resource { @Path("sub") public SubResource subresource() { - return new SubResource(); + return resourceContext.getResource(SubResource.class); } } + @Singleton public static class SubResource { @GET @@ -61,4 +63,5 @@ public String hello() { return "hello"; } } + }