Skip to content

Commit

Permalink
OpenAPI: Ignore CDI filters during buildtime
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
(cherry picked from commit fa54232)
  • Loading branch information
phillip-kruger authored and gsmet committed Jun 28, 2022
1 parent e077552 commit 8c606d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1001,20 +1001,41 @@ private OpenApiDocument storeDocument(OutputTargetBuildItem out,
OpenAPI staticModel,
OpenAPI annotationModel,
List<AddToOpenAPIDefinitionBuildItem> openAPIBuildItems) throws IOException {
return storeDocument(out, smallRyeOpenApiConfig, staticModel, annotationModel, openAPIBuildItems, true);
}

private OpenApiDocument storeDocument(OutputTargetBuildItem out,
SmallRyeOpenApiConfig smallRyeOpenApiConfig,
OpenAPI staticModel,
OpenAPI annotationModel,
List<AddToOpenAPIDefinitionBuildItem> openAPIBuildItems,
boolean includeRuntimeFilters) throws IOException {

Config config = ConfigProvider.getConfig();
OpenApiConfig openApiConfig = new OpenApiConfigImpl(config);

OpenApiDocument document = prepareOpenApiDocument(staticModel, annotationModel, openAPIBuildItems);

document.filter(filter(openApiConfig)); // This usually happens at runtime, so when storing we want to filter here too.
if (includeRuntimeFilters) {
document.filter(filter(openApiConfig)); // This usually happens at runtime, so when storing we want to filter here too.
}

// By default, also add the auto generated server
OASFilter autoServerFilter = getAutoServerFilter(smallRyeOpenApiConfig, true);
if (autoServerFilter != null) {
document.filter(autoServerFilter);
}
document.initialize();

try {
document.initialize();
} catch (RuntimeException re) {
if (includeRuntimeFilters) {
// This is a Runtime filter, so it might not work at build time. In that case we ignore the filter.
return storeDocument(out, smallRyeOpenApiConfig, staticModel, annotationModel, openAPIBuildItems, false);
} else {
throw re;
}
}
// Store the document if needed
boolean shouldStore = smallRyeOpenApiConfig.storeSchemaDirectory.isPresent();
if (shouldStore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Optional;

import javax.enterprise.inject.spi.CDI;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.openapi.OASFilter;
Expand All @@ -19,6 +21,9 @@ public void filterOpenAPI(OpenAPI openAPI) {
Optional<String> maybeVersion = config.getOptionalValue("my.openapi.version", String.class);
String version = maybeVersion.orElse("3.0.3");
openAPI.setOpenapi(version);

// Below is to test runtime filters that use CDI
CDI.current().getBeanManager().createInstance();
}

}

0 comments on commit 8c606d0

Please sign in to comment.