Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid executing OpenAPI build filters twice on build #38070

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,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 @@ -1146,7 +1146,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 @@ -1186,7 +1186,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 @@ -1203,10 +1204,12 @@ private OpenApiDocument prepareOpenApiDocument(OpenAPI staticModel,
OASFilter otherExtensionFilter = openAPIBuildItem.getOASFilter();
document.filter(otherExtensionFilter);
}
// Add user defined Build time filters
List<String> userDefinedFilters = getUserDefinedBuildtimeFilters(index);
for (String filter : userDefinedFilters) {
document.filter(filter(filter, index));
// Add user defined Build time filters if necessary
if (executeBuildFilters) {
List<String> userDefinedFilters = getUserDefinedBuildtimeFilters(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,28 @@
package io.quarkus.smallrye.openapi.test.jaxrs;

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(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)"));

}

}
Loading