diff --git a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java index e5189cc19c5a77..b5d09571f7ed3b 100644 --- a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java +++ b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java @@ -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; @@ -308,13 +309,31 @@ void additionalBean(BuildProducer 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 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 diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiBuildTimeExcludedClassTestCase.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiBuildTimeExcludedClassTestCase.java new file mode 100644 index 00000000000000..5e18e9b752aa13 --- /dev/null +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiBuildTimeExcludedClassTestCase.java @@ -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 ""; + } + } + +}