Skip to content

Commit

Permalink
Register OpenApiConfigMapping properly at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo committed Dec 23, 2022
1 parent f9050cf commit a1dbbbd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import io.quarkus.deployment.builditem.GeneratedResourceBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
Expand All @@ -79,6 +80,7 @@
import io.quarkus.resteasy.server.common.spi.AllowedJaxRsAnnotationPrefixBuildItem;
import io.quarkus.resteasy.server.common.spi.ResteasyJaxrsConfigBuildItem;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.configuration.ConfigBuilder;
import io.quarkus.runtime.util.ClassPathUtils;
import io.quarkus.security.Authenticated;
import io.quarkus.smallrye.openapi.common.deployment.SmallRyeOpenApiConfig;
Expand All @@ -89,6 +91,7 @@
import io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem;
import io.quarkus.smallrye.openapi.deployment.spi.IgnoreStaticDocumentBuildItem;
import io.quarkus.smallrye.openapi.deployment.spi.OpenApiDocumentBuildItem;
import io.quarkus.smallrye.openapi.runtime.OpenApiConfigMapping;
import io.quarkus.smallrye.openapi.runtime.OpenApiConstants;
import io.quarkus.smallrye.openapi.runtime.OpenApiDocumentService;
import io.quarkus.smallrye.openapi.runtime.OpenApiRecorder;
Expand All @@ -103,6 +106,7 @@
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.deployment.SecurityInformationBuildItem;
import io.quarkus.vertx.http.deployment.devmode.NotFoundPageDisplayableEndpointBuildItem;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.openapi.api.OpenApiConfig;
import io.smallrye.openapi.api.OpenApiConfigImpl;
import io.smallrye.openapi.api.OpenApiDocument;
Expand Down Expand Up @@ -173,11 +177,15 @@ void contributeClassesToIndex(BuildProducer<AdditionalIndexedClassesBuildItem> a

@BuildStep
void registerNativeImageResources(BuildProducer<ServiceProviderBuildItem> serviceProvider) {
// To map from smallrye and mp config to quarkus
serviceProvider.produce(ServiceProviderBuildItem.allProvidersFromClassPath(OpenApiConfigMapping.class.getName()));
serviceProvider.produce(ServiceProviderBuildItem.allProvidersFromClassPath(OASFactoryResolver.class.getName()));
}

@BuildStep
void runtimeOnly(BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {
// To map from smallrye and mp config to quarkus
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(RuntimeOnlyBuilder.class.getName()));
}

@BuildStep
void configFiles(BuildProducer<HotDeploymentWatchedFileBuildItem> watchedFiles,
SmallRyeOpenApiConfig openApiConfig,
Expand Down Expand Up @@ -1113,4 +1121,11 @@ private OASFilter filter(OpenApiConfig openApiConfig) {
return OpenApiProcessor.getFilter(openApiConfig,
Thread.currentThread().getContextClassLoader());
}

public static class RuntimeOnlyBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
return builder.withInterceptors(new OpenApiConfigMapping());
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.smallrye.openapi.deployment;
package io.quarkus.smallrye.openapi.runtime;

import java.util.Collections;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import java.nio.charset.StandardCharsets;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.openapi.OASFilter;
import org.eclipse.microprofile.openapi.models.OpenAPI;

import io.quarkus.runtime.ShutdownEvent;
import io.smallrye.openapi.api.OpenApiConfig;
import io.smallrye.openapi.api.OpenApiConfigImpl;
import io.smallrye.openapi.api.OpenApiDocument;
Expand All @@ -24,13 +26,16 @@
@ApplicationScoped
public class OpenApiDocumentService implements OpenApiDocumentHolder {

private static final String OPENAPI_SERVERS = "mp.openapi.servers";
private final OpenApiDocumentHolder documentHolder;
private final String previousOpenApiServersSystemPropertyValue;

public OpenApiDocumentService(OASFilter autoSecurityFilter, Config config) {

String servers = config.getOptionalValue("quarkus.smallrye-openapi.servers", String.class).orElse(null);
this.previousOpenApiServersSystemPropertyValue = System.getProperty(OPENAPI_SERVERS);
if (servers != null && !servers.isEmpty()) {
System.setProperty("mp.openapi.servers", servers);
System.setProperty(OPENAPI_SERVERS, servers);
}

if (config.getOptionalValue("quarkus.smallrye-openapi.always-run-filter", Boolean.class).orElse(Boolean.FALSE)) {
Expand All @@ -40,6 +45,17 @@ public OpenApiDocumentService(OASFilter autoSecurityFilter, Config config) {
}
}

void reset(@Observes ShutdownEvent event) {
// Reset the value of the System property "mp.openapi.servers" to prevent side effects on tests since
// the value of System property "mp.openapi.servers" takes precedence over the value of
// "quarkus.smallrye-openapi.servers" due to the configuration mapping
if (previousOpenApiServersSystemPropertyValue == null) {
System.clearProperty(OPENAPI_SERVERS);
} else {
System.setProperty(OPENAPI_SERVERS, previousOpenApiServersSystemPropertyValue);
}
}

public byte[] getJsonDocument() {
return this.documentHolder.getJsonDocument();
}
Expand Down

0 comments on commit a1dbbbd

Please sign in to comment.