From 930744b68f9ef94303525590ea10932788de2140 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 23 Nov 2022 10:11:40 +0200 Subject: [PATCH] Disallow conditional annotations and declarative filter annotations on methods Although we can make this work in the future, it involves a fair amount of work for a very small gain, so let's make it explicit for now that this combination is not allowed. Follows up on: #29118 --- .../ResteasyReactiveScanningProcessor.java | 9 +++ .../InvalidConditionalBeanFiltersTest.java | 62 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/customproviders/InvalidConditionalBeanFiltersTest.java diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveScanningProcessor.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveScanningProcessor.java index 32f47b768d25a..daeb11c95d6cb 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveScanningProcessor.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveScanningProcessor.java @@ -313,6 +313,15 @@ public void handleCustomAnnotatedMethods( List generatedFilters = FilterGeneration.generate(index, Set.of(HTTP_SERVER_REQUEST, HTTP_SERVER_RESPONSE, ROUTING_CONTEXT), Set.of(Unremovable.class.getName()), (methodInfo -> { + List methodAnnotations = methodInfo.annotations(); + for (AnnotationInstance methodAnnotation : methodAnnotations) { + if (BuildTimeEnabledProcessor.BUILD_TIME_ENABLED_BEAN_ANNOTATIONS.contains(methodAnnotation.name())) { + throw new RuntimeException("The combination of '@" + methodAnnotation.name().withoutPackagePrefix() + + "' and '@ServerRequestFilter' or '@ServerResponseFilter' is not allowed. Offending method is '" + + methodInfo.name() + "' of class '" + methodInfo.declaringClass().name() + "'"); + } + } + List classAnnotations = methodInfo.declaringClass().declaredAnnotations(); for (AnnotationInstance classAnnotation : classAnnotations) { if (BuildTimeEnabledProcessor.BUILD_TIME_ENABLED_BEAN_ANNOTATIONS.contains(classAnnotation.name())) { diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/customproviders/InvalidConditionalBeanFiltersTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/customproviders/InvalidConditionalBeanFiltersTest.java new file mode 100644 index 0000000000000..b101b18929538 --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/customproviders/InvalidConditionalBeanFiltersTest.java @@ -0,0 +1,62 @@ +package io.quarkus.resteasy.reactive.server.test.customproviders; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.function.Supplier; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.UriInfo; + +import org.jboss.resteasy.reactive.server.ServerRequestFilter; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.profile.IfBuildProfile; +import io.quarkus.test.QuarkusUnitTest; + +public class InvalidConditionalBeanFiltersTest { + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .setArchiveProducer(new Supplier<>() { + @Override + public JavaArchive get() { + return ShrinkWrap.create(JavaArchive.class) + .addClasses(TestResource.class, Filters.class); + } + }).assertException(t -> { + String message = t.getMessage(); + assertTrue(message.contains("@IfBuildProfile")); + assertTrue(message.contains("request")); + assertTrue(message.contains(InvalidConditionalBeanFiltersTest.Filters.class.getName())); + }); + + @Test + public void test() { + fail("Should never have been called"); + } + + @Path("test") + public static class TestResource { + + @GET + public String hello() { + return "hello"; + } + + } + + public static class Filters { + + @IfBuildProfile("test") + @ServerRequestFilter + public void request(UriInfo info) { + + } + + } +}