Skip to content

Commit

Permalink
Make Sub Resources unremovable beans
Browse files Browse the repository at this point in the history
Closes: #5314
(cherry picked from commit eb1ba43)
  • Loading branch information
geoand authored and gsmet committed Feb 27, 2024
1 parent b080758 commit e42a281
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
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";
}
}

}

0 comments on commit e42a281

Please sign in to comment.