Skip to content

Commit

Permalink
Merge pull request #30055 from essobedo/fix-open-api-config-mapping
Browse files Browse the repository at this point in the history
Register properly OpenApiConfigMapping at runtime
  • Loading branch information
radcortez authored Jan 13, 2023
2 parents a2777c3 + 2d4772c commit 5563e39
Show file tree
Hide file tree
Showing 5 changed files with 38 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 @@ -93,6 +94,7 @@
import io.quarkus.smallrye.openapi.runtime.OpenApiDocumentService;
import io.quarkus.smallrye.openapi.runtime.OpenApiRecorder;
import io.quarkus.smallrye.openapi.runtime.OpenApiRuntimeConfig;
import io.quarkus.smallrye.openapi.runtime.RuntimeOnlyBuilder;
import io.quarkus.smallrye.openapi.runtime.filter.AutoBasicSecurityFilter;
import io.quarkus.smallrye.openapi.runtime.filter.AutoBearerTokenSecurityFilter;
import io.quarkus.smallrye.openapi.runtime.filter.AutoUrl;
Expand Down Expand Up @@ -173,11 +175,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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.quarkus.smallrye.openapi.deployment.OpenApiConfigMapping
io.quarkus.smallrye.openapi.runtime.OpenApiConfigMapping
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.smallrye.openapi.runtime;

import io.quarkus.runtime.configuration.ConfigBuilder;
import io.smallrye.config.SmallRyeConfigBuilder;

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

0 comments on commit 5563e39

Please sign in to comment.