Skip to content

Commit

Permalink
Merge pull request #20534 from geoand/#20527
Browse files Browse the repository at this point in the history
Fix race condition in OpenAPI bean handling
  • Loading branch information
geoand authored Oct 5, 2021
2 parents 679eb30 + dc86d79 commit 12f7e3e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanArchiveIndexBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.Feature;
Expand Down Expand Up @@ -171,6 +172,7 @@ List<HotDeploymentWatchedFileBuildItem> configFiles() {
@Record(ExecutionTime.RUNTIME_INIT)
RouteBuildItem handler(LaunchModeBuildItem launch,
BuildProducer<NotFoundPageDisplayableEndpointBuildItem> displayableEndpoints,
BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
OpenApiRecorder recorder,
NonApplicationRootPathBuildItem nonApplicationRootPathBuildItem,
List<SecurityInformationBuildItem> securityInformationBuildItems,
Expand Down Expand Up @@ -204,7 +206,10 @@ RouteBuildItem handler(LaunchModeBuildItem launch,
}
}

Handler<RoutingContext> handler = recorder.handler(openApiRuntimeConfig, httpConfiguration, autoSecurityFilter);
syntheticBeans.produce(SyntheticBeanBuildItem.configure(OASFilter.class).setRuntimeInit()
.supplier(recorder.autoSecurityFilterSupplier(autoSecurityFilter)).done());

Handler<RoutingContext> handler = recorder.handler(openApiRuntimeConfig, httpConfiguration);
return nonApplicationRootPathBuildItem.routeBuilder()
.route(openApiConfig.path)
.routeConfigKey("quarkus.smallrye-openapi.path")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import javax.enterprise.context.ApplicationScoped;

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

Expand All @@ -25,16 +24,10 @@
@ApplicationScoped
public class OpenApiDocumentService implements OpenApiDocumentHolder {

private boolean alwaysRunFilter;
private final OpenApiDocumentHolder documentHolder;

private OpenApiDocumentHolder documentHolder;

void init(OASFilter autoSecurityFilter) {

Config config = ConfigProvider.getConfig();
this.alwaysRunFilter = config.getOptionalValue("quarkus.smallrye-openapi.always-run-filter", Boolean.class)
.orElse(Boolean.FALSE);
if (alwaysRunFilter) {
public OpenApiDocumentService(OASFilter autoSecurityFilter, Config config) {
if (config.getOptionalValue("quarkus.smallrye-openapi.always-run-filter", Boolean.class).orElse(Boolean.FALSE)) {
this.documentHolder = new DynamicDocument(config, autoSecurityFilter);
} else {
this.documentHolder = new StaticDocument(config, autoSecurityFilter);
Expand All @@ -52,7 +45,7 @@ public byte[] getYamlDocument() {
/**
* Generate the document once on creation.
*/
class StaticDocument implements OpenApiDocumentHolder {
static class StaticDocument implements OpenApiDocumentHolder {

private byte[] jsonDocument;
private byte[] yamlDocument;
Expand Down Expand Up @@ -101,7 +94,7 @@ public byte[] getYamlDocument() {
/**
* Generate the document on every request.
*/
class DynamicDocument implements OpenApiDocumentHolder {
static class DynamicDocument implements OpenApiDocumentHolder {

private OpenAPI generatedOnBuild;
private OpenApiConfig openApiConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.List;

import org.eclipse.microprofile.openapi.OASFilter;

import io.quarkus.arc.Arc;
import io.smallrye.openapi.runtime.io.Format;
import io.vertx.core.Handler;
Expand Down Expand Up @@ -34,11 +32,9 @@ public class OpenApiHandler implements Handler<RoutingContext> {
}

final boolean corsEnabled;
final OASFilter autoSecurityFilter;

public OpenApiHandler(boolean corsEnabled, OASFilter autoSecurityFilter) {
public OpenApiHandler(boolean corsEnabled) {
this.corsEnabled = corsEnabled;
this.autoSecurityFilter = autoSecurityFilter;
}

@Override
Expand Down Expand Up @@ -81,7 +77,6 @@ public void handle(RoutingContext event) {
private OpenApiDocumentService getOpenApiDocumentService() {
if (this.openApiDocumentService == null) {
this.openApiDocumentService = Arc.container().instance(OpenApiDocumentService.class).get();
this.openApiDocumentService.init(this.autoSecurityFilter);
}
return this.openApiDocumentService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.function.Supplier;

import org.eclipse.microprofile.openapi.OASFilter;
import org.eclipse.microprofile.openapi.spi.OASFactoryResolver;
Expand All @@ -17,10 +18,9 @@
@Recorder
public class OpenApiRecorder {

public Handler<RoutingContext> handler(OpenApiRuntimeConfig runtimeConfig, HttpConfiguration configuration,
OASFilter autoSecurityFilter) {
public Handler<RoutingContext> handler(OpenApiRuntimeConfig runtimeConfig, HttpConfiguration configuration) {
if (runtimeConfig.enable) {
return new OpenApiHandler(configuration.corsEnabled, autoSecurityFilter);
return new OpenApiHandler(configuration.corsEnabled);
} else {
return new OpenApiNotFoundHandler();
}
Expand Down Expand Up @@ -77,4 +77,13 @@ public InputStream getResourceAsStream(String name) {
}

}

public Supplier<OASFilter> autoSecurityFilterSupplier(OASFilter autoSecurityFilter) {
return new Supplier<>() {
@Override
public OASFilter get() {
return autoSecurityFilter;
}
};
}
}

0 comments on commit 12f7e3e

Please sign in to comment.