Skip to content

Commit

Permalink
Merge pull request #29432 from geoand/#29118-followup
Browse files Browse the repository at this point in the history
Improve support for conditional bean and declarative filter annotations combination
  • Loading branch information
geoand authored Nov 23, 2022
2 parents 7f4f141 + 3a5ad2d commit af7bf36
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -31,6 +32,9 @@ public class LookupConditionsProcessor {
private static final DotName LOOK_UP_UNLESS_PROPERTY_CONTAINER = DotName
.createSimple(LookupUnlessProperty.List.class.getName());

public static final Set<DotName> LOOKUP_BEAN_ANNOTATIONS = Set.of(LOOK_UP_IF_PROPERTY, LOOK_UP_IF_CONTAINER,
LOOK_UP_UNLESS_PROPERTY, LOOK_UP_UNLESS_PROPERTY_CONTAINER);

private static final String NAME = "name";
private static final String STRING_VALUE = "stringValue";
private static final String LOOKUP_IF_MISSING = "lookupIfMissing";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.quarkus.arc.deployment.BuildTimeEnabledProcessor;
import io.quarkus.arc.deployment.GeneratedBeanBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanGizmoAdaptor;
import io.quarkus.arc.deployment.LookupConditionsProcessor;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
Expand Down Expand Up @@ -81,6 +82,13 @@
*/
public class ResteasyReactiveScanningProcessor {

public static final Set<DotName> CONDITIONAL_BEAN_ANNOTATIONS;

static {
CONDITIONAL_BEAN_ANNOTATIONS = new HashSet<>(BuildTimeEnabledProcessor.BUILD_TIME_ENABLED_BEAN_ANNOTATIONS);
CONDITIONAL_BEAN_ANNOTATIONS.addAll(LookupConditionsProcessor.LOOKUP_BEAN_ANNOTATIONS);
}

@BuildStep
public MethodScannerBuildItem asyncSupport() {
return new MethodScannerBuildItem(new AsyncReturnTypeScanner());
Expand Down Expand Up @@ -313,9 +321,18 @@ public void handleCustomAnnotatedMethods(
List<FilterGeneration.GeneratedFilter> generatedFilters = FilterGeneration.generate(index,
Set.of(HTTP_SERVER_REQUEST, HTTP_SERVER_RESPONSE, ROUTING_CONTEXT), Set.of(Unremovable.class.getName()),
(methodInfo -> {
List<AnnotationInstance> methodAnnotations = methodInfo.annotations();
for (AnnotationInstance methodAnnotation : methodAnnotations) {
if (CONDITIONAL_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<AnnotationInstance> classAnnotations = methodInfo.declaringClass().declaredAnnotations();
for (AnnotationInstance classAnnotation : classAnnotations) {
if (BuildTimeEnabledProcessor.BUILD_TIME_ENABLED_BEAN_ANNOTATIONS.contains(classAnnotation.name())) {
if (CONDITIONAL_BEAN_ANNOTATIONS.contains(classAnnotation.name())) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.lookup.LookupIfProperty;
import io.quarkus.arc.lookup.LookupUnlessProperty;
import io.quarkus.arc.profile.IfBuildProfile;
import io.quarkus.arc.properties.IfBuildProperty;
import io.quarkus.test.QuarkusUnitTest;
Expand All @@ -49,14 +51,14 @@ public JavaArchive get() {
public void testExpectedFilters() {
List<String> responseFiltersValues = get("/test/filters")
.then().statusCode(200)
.body(Matchers.is("void-on,response-on,uni-on,always"))
.body(Matchers.is("void-on,response-on,uni-on,void-lookup-on,always"))
.extract()
.headers()
.getList("response-filters")
.stream()
.map(Header::getValue)
.collect(Collectors.toList());
assertThat(responseFiltersValues).containsOnly("always", "void-on", "uni-on");
assertThat(responseFiltersValues).containsOnly("always", "void-lookup-on", "void-on", "uni-on");
}

@Path("test")
Expand Down Expand Up @@ -135,6 +137,34 @@ public Uni<Void> uniResponseFilter(ContainerResponseContext ctx) {
}
}

@LookupIfProperty(name = "notexistingproperty", stringValue = "true")
public static class WontBeEnabledLookupPropertyFilter {

@ServerRequestFilter(priority = Priorities.USER + 10)
public void voidRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "void-lookup-off");
}

@ServerResponseFilter
public void voidResponseFilter(ContainerResponseContext ctx) {
assertFalse(true);
}
}

@LookupUnlessProperty(name = "notexistingproperty", stringValue = "true", lookupIfMissing = true)
public static class WillBeEnabledLookupPropertyFilter {

@ServerRequestFilter(priority = Priorities.USER + 20)
public void voidRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "void-lookup-on");
}

@ServerResponseFilter(priority = Priorities.USER + 20)
public void voidResponseFilter(ContainerResponseContext ctx) {
ctx.getHeaders().add("response-filters", "void-lookup-on");
}
}

@Singleton
public static class AlwaysEnabledFilter {

Expand Down
Original file line number Diff line number Diff line change
@@ -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) {

}

}
}

0 comments on commit af7bf36

Please sign in to comment.