Skip to content

Commit

Permalink
OpenAPI: ignore build-time excluded classes from annotation scan
Browse files Browse the repository at this point in the history
Fixes #16218

Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar committed Mar 25, 2023
1 parent f017f9f commit b2c9e9a
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanArchiveIndexBuildItem;
import io.quarkus.arc.deployment.BuildExclusionsBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
Expand Down Expand Up @@ -308,13 +309,31 @@ void additionalBean(BuildProducer<AdditionalBeanBuildItem> additionalBeanProduce

@BuildStep
OpenApiFilteredIndexViewBuildItem smallryeOpenApiIndex(CombinedIndexBuildItem combinedIndexBuildItem,
BeanArchiveIndexBuildItem beanArchiveIndexBuildItem) {
CompositeIndex compositeIndex = CompositeIndex.create(combinedIndexBuildItem.getIndex(),
BeanArchiveIndexBuildItem beanArchiveIndexBuildItem,
BuildExclusionsBuildItem buildExclusionsBuildItem) {

CompositeIndex compositeIndex = CompositeIndex.create(
combinedIndexBuildItem.getIndex(),
beanArchiveIndexBuildItem.getIndex());
return new OpenApiFilteredIndexViewBuildItem(
new FilteredIndexView(
compositeIndex,
new OpenApiConfigImpl(ConfigProvider.getConfig())));

OpenApiConfig config = OpenApiConfig.fromConfig(ConfigProvider.getConfig());
Set<DotName> buildTimeClassExclusions = buildExclusionsBuildItem.getExcludedDeclaringClasses()
.stream()
.map(DotName::createSimple)
.collect(Collectors.toSet());

FilteredIndexView indexView = new FilteredIndexView(compositeIndex, config) {
@Override
public boolean accepts(DotName className) {
if (super.accepts(className)) {
return !buildTimeClassExclusions.contains(className);
}

return false;
}
};

return new OpenApiFilteredIndexViewBuildItem(indexView);
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package io.quarkus.smallrye.openapi.test.jaxrs;

import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.profile.IfBuildProfile;
import io.quarkus.arc.profile.UnlessBuildProfile;
import io.quarkus.arc.properties.IfBuildProperty;
import io.quarkus.arc.properties.UnlessBuildProperty;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

class OpenApiBuildTimeExcludedClassTestCase {

static String quarkusProfile;

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(IfBuildProfileTest.class,
IfBuildProfileBar.class,
IfBuildPropertyBarBazIsTrue.class,
IfBuildProperyFooBarIsTrue.class,
UnlessBuildProfileBar.class,
UnlessBuildProfileTest.class,
UnlessBuildPropertyBarBazIsFalse.class,
UnlessBuildProperyFooBarIsFalse.class)
.addAsResource(
new StringAsset("%test.foobar=true\n"
+ "%test.barbaz=false\n"
+ "foobar=false\n"
+ "barbaz=true\n"),
"application.properties"));

@Test
void testAutoSecurityRequirement() {
RestAssured.given()
.header("Accept", "application/json")
.when()
.get("/q/openapi")
.then()
.log().body()
.body("paths", aMapWithSize(4))

.body("paths", hasKey("/test-profile-enabled"))
.body("paths", not(hasKey("/test-profile-not-enabled")))
.body("paths", hasKey("/bar-profile-not-enabled"))
.body("paths", not(hasKey("/bar-profile-enabled")))

.body("paths", hasKey("/foobar-property-true"))
.body("paths", hasKey("/foobar-property-not-false"))
.body("paths", not(hasKey("/barbaz-property-true")))
.body("paths", not(hasKey("/barbaz-property-not-false")));
}

@Path("/test-profile-enabled")
@IfBuildProfile("test")
public static class IfBuildProfileTest {
@GET
public String endpoint() {
return "";
}
}

@Path("/bar-profile-enabled")
@IfBuildProfile("bar")
public static class IfBuildProfileBar {
@GET
public String endpoint() {
return "";
}
}

@Path("/test-profile-not-enabled")
@UnlessBuildProfile("test")
public static class UnlessBuildProfileTest {
@GET
public String endpoint() {
return "";
}
}

@Path("/bar-profile-not-enabled")
@UnlessBuildProfile("bar")
public static class UnlessBuildProfileBar {
@GET
public String endpoint() {
return "";
}
}

@Path("/foobar-property-true")
@IfBuildProperty(name = "foobar", stringValue = "true", enableIfMissing = false)
public static class IfBuildProperyFooBarIsTrue {
@GET
public String endpoint() {
return "";
}
}

@Path("/barbaz-property-true")
@IfBuildProperty(name = "barbaz", stringValue = "true", enableIfMissing = false)
public static class IfBuildPropertyBarBazIsTrue {
@GET
public String endpoint() {
return "";
}
}

@Path("/foobar-property-not-false")
@UnlessBuildProperty(name = "foobar", stringValue = "false", enableIfMissing = false)
public static class UnlessBuildProperyFooBarIsFalse {
@GET
public String endpoint() {
return "";
}
}

@Path("/barbaz-property-not-false")
@UnlessBuildProperty(name = "barbaz", stringValue = "false", enableIfMissing = false)
public static class UnlessBuildPropertyBarBazIsFalse {
@GET
public String endpoint() {
return "";
}
}

}

0 comments on commit b2c9e9a

Please sign in to comment.