From 8c606d0216b5ad76398d97b6b1a485db21d6e4e1 Mon Sep 17 00:00:00 2001 From: Phillip Kruger Date: Tue, 28 Jun 2022 10:58:39 +1000 Subject: [PATCH] OpenAPI: Ignore CDI filters during buildtime Signed-off-by: Phillip Kruger (cherry picked from commit fa54232e61f760d0c050cb760853c59729d1d381) --- .../deployment/SmallRyeOpenApiProcessor.java | 25 +++++++++++++++++-- .../openapi/test/jaxrs/MyOASFilter.java | 5 ++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java index 29988780630ac..1c8a4dc310daf 100644 --- a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java +++ b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java @@ -1001,20 +1001,41 @@ private OpenApiDocument storeDocument(OutputTargetBuildItem out, OpenAPI staticModel, OpenAPI annotationModel, List openAPIBuildItems) throws IOException { + return storeDocument(out, smallRyeOpenApiConfig, staticModel, annotationModel, openAPIBuildItems, true); + } + + private OpenApiDocument storeDocument(OutputTargetBuildItem out, + SmallRyeOpenApiConfig smallRyeOpenApiConfig, + OpenAPI staticModel, + OpenAPI annotationModel, + List 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) { diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/MyOASFilter.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/MyOASFilter.java index 836d943232a6d..227e15567a630 100644 --- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/MyOASFilter.java +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/MyOASFilter.java @@ -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; @@ -19,6 +21,9 @@ public void filterOpenAPI(OpenAPI openAPI) { Optional 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(); } }