Skip to content

Commit

Permalink
Merge pull request #40812 from pcheucle/hide-lra-swagger
Browse files Browse the repository at this point in the history
Make LRA proxy endpoints optional in OpenAPI
  • Loading branch information
gsmet authored Jun 6, 2024
2 parents d7e022b + 3eb4cd4 commit b65d33d
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
29 changes: 29 additions & 0 deletions extensions/narayana-lra/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi-spi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

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

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -153,4 +154,14 @@ void registerBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
.build());
additionalBeans.produce(new AdditionalBeanBuildItem(NarayanaLRAProducers.class));
}

@BuildStep
public void filterOpenAPIEndpoint(BuildProducer<AddToOpenAPIDefinitionBuildItem> openAPIProducer,
Capabilities capabilities, LRABuildTimeConfiguration lraBuildTimeConfig) {

if (capabilities.isPresent(Capability.SMALLRYE_OPENAPI)) {
NarayanaLRAOpenAPIFilter lraOpenAPIFilter = new NarayanaLRAOpenAPIFilter(lraBuildTimeConfig.openapiIncluded);
openAPIProducer.produce(new AddToOpenAPIDefinitionBuildItem(lraOpenAPIFilter));
}
}
}
Original file line number Diff line number Diff line change
@@ -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")));
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit b65d33d

Please sign in to comment.