diff --git a/extensions/narayana-lra/deployment/pom.xml b/extensions/narayana-lra/deployment/pom.xml index 88f66426dd071..e7b6565cd24af 100644 --- a/extensions/narayana-lra/deployment/pom.xml +++ b/extensions/narayana-lra/deployment/pom.xml @@ -21,6 +21,35 @@ io.quarkus quarkus-arc-deployment + + io.quarkus + quarkus-smallrye-openapi-spi + + + io.quarkus + quarkus-junit5-internal + test + + + io.quarkus + quarkus-rest-jackson-deployment + test + + + io.quarkus + quarkus-rest-client-deployment + test + + + io.quarkus + quarkus-smallrye-openapi-deployment + test + + + io.rest-assured + rest-assured + test + diff --git a/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/LRABuildTimeConfiguration.java b/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/LRABuildTimeConfiguration.java new file mode 100644 index 0000000000000..b694109c91e6c --- /dev/null +++ b/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/LRABuildTimeConfiguration.java @@ -0,0 +1,18 @@ +package io.quarkus.narayana.lra.deployment; + +import io.quarkus.runtime.annotations.ConfigItem; +import io.quarkus.runtime.annotations.ConfigPhase; +import io.quarkus.runtime.annotations.ConfigRoot; + +/** + * LRA build time configuration properties + */ +@ConfigRoot(phase = ConfigPhase.BUILD_TIME) +public final class LRABuildTimeConfiguration { + + /** + * Whether to include LRA proxy endpoints in the generated OpenAPI document + */ + @ConfigItem(name = "openapi.included", defaultValue = "false") + public boolean openapiIncluded; +} diff --git a/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/NarayanaLRAOpenAPIFilter.java b/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/NarayanaLRAOpenAPIFilter.java new file mode 100644 index 0000000000000..3c509400e7406 --- /dev/null +++ b/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/NarayanaLRAOpenAPIFilter.java @@ -0,0 +1,48 @@ +package io.quarkus.narayana.lra.deployment; + +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.microprofile.openapi.OASFilter; +import org.eclipse.microprofile.openapi.models.OpenAPI; +import org.eclipse.microprofile.openapi.models.Paths; + +public class NarayanaLRAOpenAPIFilter implements OASFilter { + + final static String LRA_PROXY_PATH = "/lraproxy"; + final static String LRA_PARTICIAPANT_PROXY_PATH = "/lra-participant-proxy"; + final static String LRA_STATUS_SCHEMA = "LRAStatus"; + + boolean openapiIncluded; + + public NarayanaLRAOpenAPIFilter(boolean openapiIncluded) { + this.openapiIncluded = openapiIncluded; + } + + @Override + public void filterOpenAPI(OpenAPI openAPI) { + + Paths paths = openAPI.getPaths(); + + if (!openapiIncluded) { + + Set lraProxyPaths = new HashSet<>(); + + for (String path : paths.getPathItems().keySet()) { + if (path.startsWith(LRA_PROXY_PATH) || path.startsWith(LRA_PARTICIAPANT_PROXY_PATH)) { + lraProxyPaths.add(path); + } + } + + // remove LRA proxy paths from OpenAPI + for (String path : lraProxyPaths) { + paths.removePathItem(path); + } + + // remove LRA schema from OpenAPI + openAPI.getComponents().removeSchema(LRA_STATUS_SCHEMA); + } + + } + +} diff --git a/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/NarayanaLRAProcessor.java b/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/NarayanaLRAProcessor.java index a3aeebc87b5fc..5a702ddd986b4 100644 --- a/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/NarayanaLRAProcessor.java +++ b/extensions/narayana-lra/deployment/src/main/java/io/quarkus/narayana/lra/deployment/NarayanaLRAProcessor.java @@ -30,6 +30,7 @@ import io.quarkus.narayana.lra.runtime.LRAConfiguration; import io.quarkus.narayana.lra.runtime.NarayanaLRAProducers; import io.quarkus.narayana.lra.runtime.NarayanaLRARecorder; +import io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem; class NarayanaLRAProcessor { @@ -153,4 +154,14 @@ void registerBeans(BuildProducer additionalBeans) { .build()); additionalBeans.produce(new AdditionalBeanBuildItem(NarayanaLRAProducers.class)); } + + @BuildStep + public void filterOpenAPIEndpoint(BuildProducer openAPIProducer, + Capabilities capabilities, LRABuildTimeConfiguration lraBuildTimeConfig) { + + if (capabilities.isPresent(Capability.SMALLRYE_OPENAPI)) { + NarayanaLRAOpenAPIFilter lraOpenAPIFilter = new NarayanaLRAOpenAPIFilter(lraBuildTimeConfig.openapiIncluded); + openAPIProducer.produce(new AddToOpenAPIDefinitionBuildItem(lraOpenAPIFilter)); + } + } } diff --git a/extensions/narayana-lra/deployment/src/test/java/io/quarkus/narayana/lra/test/LRAOpenAPIExcludedTest.java b/extensions/narayana-lra/deployment/src/test/java/io/quarkus/narayana/lra/test/LRAOpenAPIExcludedTest.java new file mode 100644 index 0000000000000..a020f81dd0fbd --- /dev/null +++ b/extensions/narayana-lra/deployment/src/test/java/io/quarkus/narayana/lra/test/LRAOpenAPIExcludedTest.java @@ -0,0 +1,26 @@ +package io.quarkus.narayana.lra.test; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.not; + +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class LRAOpenAPIExcludedTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest().withApplicationRoot((jar) -> jar + .addAsResource(new StringAsset("quarkus.lra.openapi.included=false"), + "application.properties")); + + @Test + public void testLRAExcluded() { + RestAssured.when().get("/q/openapi").then() + .body(not(containsString("lraproxy")), not(containsString("lra-participant-proxy")), + not(containsString("LRAStatus"))); + } +} diff --git a/extensions/narayana-lra/deployment/src/test/java/io/quarkus/narayana/lra/test/LRAOpenAPIIncludedTest.java b/extensions/narayana-lra/deployment/src/test/java/io/quarkus/narayana/lra/test/LRAOpenAPIIncludedTest.java new file mode 100644 index 0000000000000..50a77f4628fd5 --- /dev/null +++ b/extensions/narayana-lra/deployment/src/test/java/io/quarkus/narayana/lra/test/LRAOpenAPIIncludedTest.java @@ -0,0 +1,24 @@ +package io.quarkus.narayana.lra.test; + +import static org.hamcrest.CoreMatchers.containsString; + +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class LRAOpenAPIIncludedTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest().withApplicationRoot((jar) -> jar + .addAsResource(new StringAsset("quarkus.lra.openapi.included=true"), + "application.properties")); + + @Test + public void testLRAIncluded() { + RestAssured.when().get("/q/openapi").then() + .body(containsString("lraproxy"), containsString("lra-participant-proxy"), containsString("LRAStatus")); + } +}