Skip to content

Commit

Permalink
Evict twice callings on build OpenAPI documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Jan 8, 2024
1 parent ba97483 commit a8ecb2f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ static class Result {

private OpenApiDocument loadDocument(OpenAPI staticModel, OpenAPI annotationModel,
List<AddToOpenAPIDefinitionBuildItem> openAPIBuildItems, IndexView index) {
OpenApiDocument document = prepareOpenApiDocument(staticModel, annotationModel, openAPIBuildItems, index);
OpenApiDocument document = prepareOpenApiDocument(staticModel, annotationModel, openAPIBuildItems, index, true);

Config c = ConfigProvider.getConfig();
String title = c.getOptionalValue("quarkus.application.name", String.class).orElse("Generated");
Expand Down Expand Up @@ -1155,7 +1155,7 @@ private OpenApiDocument storeDocument(OutputTargetBuildItem out,
Config config = ConfigProvider.getConfig();
OpenApiConfig openApiConfig = new OpenApiConfigImpl(config);

OpenApiDocument document = prepareOpenApiDocument(loadedModel, null, Collections.emptyList(), index);
OpenApiDocument document = prepareOpenApiDocument(loadedModel, null, Collections.emptyList(), index, false);

if (includeRuntimeFilters) {
List<String> userDefinedRuntimeFilters = getUserDefinedRuntimeFilters(openApiConfig, index);
Expand Down Expand Up @@ -1195,7 +1195,8 @@ private OpenApiDocument storeDocument(OutputTargetBuildItem out,
private OpenApiDocument prepareOpenApiDocument(OpenAPI staticModel,
OpenAPI annotationModel,
List<AddToOpenAPIDefinitionBuildItem> openAPIBuildItems,
IndexView index) {
IndexView index,
Boolean executeBuildFilters) {
Config config = ConfigProvider.getConfig();
OpenApiConfig openApiConfig = new OpenApiConfigImpl(config);

Expand All @@ -1213,9 +1214,12 @@ private OpenApiDocument prepareOpenApiDocument(OpenAPI staticModel,
document.filter(otherExtensionFilter);
}
// Add user defined Build time filters
List<String> userDefinedFilters = getUserDefinedBuildtimeFilters(openApiConfig, index);
for (String filter : userDefinedFilters) {
document.filter(filter(filter, index));

if (executeBuildFilters) {
List<String> userDefinedFilters = getUserDefinedBuildtimeFilters(openApiConfig, index);
for (String filter : userDefinedFilters) {
document.filter(filter(filter, index));
}
}
return document;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.smallrye.openapi.test.jaxrs;

import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.microprofile.openapi.OASFactory;
import org.eclipse.microprofile.openapi.OASFilter;
import org.eclipse.microprofile.openapi.models.OpenAPI;

import io.quarkus.smallrye.openapi.OpenApiFilter;

@OpenApiFilter(OpenApiFilter.RunStage.BUILD)
public class CounterBuildtimeFilter implements OASFilter {

private static final AtomicInteger TIMES = new AtomicInteger();

public CounterBuildtimeFilter() {
}

@Override
public void filterOpenAPI(OpenAPI aOpenAPI) {
int times = TIMES.incrementAndGet();
aOpenAPI.info(
OASFactory.createInfo().description("CounterBuildtimeFilter was called " + times + " time(s)"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.smallrye.openapi.test.jaxrs;

import java.util.concurrent.atomic.AtomicInteger;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class OpenApiAnnotatedCounterBuildtimeFilterTestCase {
private static final String OPEN_API_PATH = "/q/openapi";

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(AtomicInteger.class, OpenApiResource.class, ResourceBean.class, CounterBuildtimeFilter.class));

@Test
public void testOpenApiFilterResource() {
RestAssured.given().header("Accept", "application/json")
.when().get(OPEN_API_PATH)
.then()
.header("Content-Type", "application/json;charset=UTF-8")
.body("info.description", Matchers.startsWith("CounterBuildtimeFilter was called 1 time(s)"));

}

}

0 comments on commit a8ecb2f

Please sign in to comment.