Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the need for @Unremoveable producer with custom @Context types #33793

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package io.quarkus.resteasy.reactive.server.deployment;

import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.APPLICATION_PATH;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.CONTEXT;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.PATH;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.PROVIDER;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import jakarta.ws.rs.BeanParam;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames;
import org.jboss.resteasy.reactive.common.processor.scanning.ResourceScanningResult;
import org.jboss.resteasy.reactive.server.injection.ContextProducers;
import org.jboss.resteasy.reactive.server.processor.util.ResteasyReactiveServerDotNames;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.AutoInjectAnnotationBuildItem;
import io.quarkus.arc.deployment.BeanDefiningAnnotationBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -45,15 +53,38 @@ AutoInjectAnnotationBuildItem contextInjection(
@BuildStep
void beanDefiningAnnotations(BuildProducer<BeanDefiningAnnotationBuildItem> beanDefiningAnnotations) {
beanDefiningAnnotations
.produce(new BeanDefiningAnnotationBuildItem(ResteasyReactiveDotNames.PATH, BuiltinScope.SINGLETON.getName()));
.produce(new BeanDefiningAnnotationBuildItem(PATH, BuiltinScope.SINGLETON.getName()));
beanDefiningAnnotations
.produce(new BeanDefiningAnnotationBuildItem(ResteasyReactiveDotNames.APPLICATION_PATH,
.produce(new BeanDefiningAnnotationBuildItem(APPLICATION_PATH,
BuiltinScope.SINGLETON.getName()));
beanDefiningAnnotations
.produce(new BeanDefiningAnnotationBuildItem(ResteasyReactiveDotNames.PROVIDER,
.produce(new BeanDefiningAnnotationBuildItem(PROVIDER,
BuiltinScope.SINGLETON.getName()));
}

@BuildStep
void unremovableContextMethodParams(Optional<ResourceScanningResultBuildItem> resourceScanningResultBuildItem,
BuildProducer<UnremovableBeanBuildItem> producer) {

if (resourceScanningResultBuildItem.isEmpty()) {
return;
}

Collection<ClassInfo> resourceClasses = resourceScanningResultBuildItem.get().getResult().getScannedResources()
.values();
for (ClassInfo resourceClass : resourceClasses) {
if (resourceClass.annotationsMap().containsKey(CONTEXT)) {
for (AnnotationInstance instance : resourceClass.annotationsMap().get(CONTEXT)) {
if (instance.target().kind() != AnnotationTarget.Kind.METHOD_PARAMETER) {
continue;
}
producer.produce(UnremovableBeanBuildItem
.beanTypes(instance.target().asMethodParameter().type().name()));
}
}
}
}

@BuildStep
void subResourcesAsBeans(ResourceScanningResultBuildItem setupEndpointsResult,
List<SubResourcesAsBeansBuildItem> subResourcesAsBeans,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.quarkus.resteasy.reactive.server.test.simple;

import static org.hamcrest.CoreMatchers.is;

import java.util.function.Supplier;

import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Singleton;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;

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.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class CustomContextTypeTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(TestResource.class, CustomType.class, CustomTypeProducer.class);
}
});

@Test
public void firstTest() {
RestAssured.given().headers("foo", "bar")
.get("/test")
.then().statusCode(200).body(is("bar"));
}

@Test
public void secondTest() {
RestAssured.given().headers("foo", "baz")
.get("/test")
.then().statusCode(200).body(is("baz"));
}

@Path("/test")
public static class TestResource {

@GET
public String get(@Context CustomType customType) {
return customType.getValue();
}
}

public static class CustomType {
private final String value;

public CustomType(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

@Singleton
public static class CustomTypeProducer {

@Produces
@RequestScoped
public CustomType customType(HttpHeaders headers) {
return new CustomType(headers.getHeaderString("foo"));
}
}
}