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

Make Sub Resources unremovable beans #39028

Merged
merged 1 commit into from
Feb 27, 2024
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
Expand Up @@ -88,16 +88,26 @@ void unremovableContextMethodParams(Optional<ResourceScanningResultBuildItem> re
@BuildStep
void subResourcesAsBeans(ResourceScanningResultBuildItem setupEndpointsResult,
List<SubResourcesAsBeansBuildItem> subResourcesAsBeans,
BuildProducer<AdditionalBeanBuildItem> producer) {
if (subResourcesAsBeans.isEmpty() || setupEndpointsResult.getResult().getPossibleSubResources().isEmpty()) {
BuildProducer<UnremovableBeanBuildItem> unremovableProducer,
BuildProducer<AdditionalBeanBuildItem> additionalProducer) {
Map<DotName, ClassInfo> possibleSubResources = setupEndpointsResult.getResult().getPossibleSubResources();
if (possibleSubResources.isEmpty()) {
return;
}

List<String> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,15 +51,17 @@ public static class Resource {

@Path("sub")
public SubResource subresource() {
return new SubResource();
return resourceContext.getResource(SubResource.class);
}
}

@Singleton
public static class SubResource {

@GET
public String hello() {
return "hello";
}
}

}
Loading