From ddd06271b21a23ece64494e2f2cf241ebd1396b1 Mon Sep 17 00:00:00 2001 From: domaindrivendev Date: Mon, 18 Jul 2022 15:59:13 +0100 Subject: [PATCH] Ensure document filters can affect inferred security schemes --- .../SwaggerGenerator/SwaggerGenerator.cs | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index d54369f191..d20fbf83a5 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -41,19 +41,41 @@ public SwaggerGenerator( public async Task GetSwaggerAsync(string documentName, string host = null, string basePath = null) { - var (applicableApiDescriptions, swaggerDoc, schemaRepository) = GetSwaggerDocument(documentName, host, basePath); + var (applicableApiDescriptions, swaggerDoc, schemaRepository) = GetSwaggerDocumentWithoutFilters(documentName, host, basePath); + swaggerDoc.Components.SecuritySchemes = await GetSecuritySchemes(); + + // NOTE: Filter processing moved here so they may effect generated security schemes + var filterContext = new DocumentFilterContext(applicableApiDescriptions, _schemaGenerator, schemaRepository); + foreach (var filter in _options.DocumentFilters) + { + filter.Apply(swaggerDoc, filterContext); + } + + swaggerDoc.Components.Schemas = new SortedDictionary(swaggerDoc.Components.Schemas, _options.SchemaComparer); + return swaggerDoc; } public OpenApiDocument GetSwagger(string documentName, string host = null, string basePath = null) { - var (applicableApiDescriptions, swaggerDoc, schemaRepository) = GetSwaggerDocument(documentName, host, basePath); + var (applicableApiDescriptions, swaggerDoc, schemaRepository) = GetSwaggerDocumentWithoutFilters(documentName, host, basePath); + swaggerDoc.Components.SecuritySchemes = GetSecuritySchemes().Result; + + // NOTE: Filter processing moved here so they may effect generated security schemes + var filterContext = new DocumentFilterContext(applicableApiDescriptions, _schemaGenerator, schemaRepository); + foreach (var filter in _options.DocumentFilters) + { + filter.Apply(swaggerDoc, filterContext); + } + + swaggerDoc.Components.Schemas = new SortedDictionary(swaggerDoc.Components.Schemas, _options.SchemaComparer); + return swaggerDoc; } - private (IEnumerable, OpenApiDocument, SchemaRepository) GetSwaggerDocument(string documentName, string host = null, string basePath = null) + private (IEnumerable, OpenApiDocument, SchemaRepository) GetSwaggerDocumentWithoutFilters(string documentName, string host = null, string basePath = null) { if (!_options.SwaggerDocs.TryGetValue(documentName, out OpenApiInfo info)) throw new UnknownSwaggerDocument(documentName, _options.SwaggerDocs.Select(d => d.Key)); @@ -77,14 +99,6 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin SecurityRequirements = new List(_options.SecurityRequirements) }; - var filterContext = new DocumentFilterContext(applicableApiDescriptions, _schemaGenerator, schemaRepository); - foreach (var filter in _options.DocumentFilters) - { - filter.Apply(swaggerDoc, filterContext); - } - - swaggerDoc.Components.Schemas = new SortedDictionary(swaggerDoc.Components.Schemas, _options.SchemaComparer); - return (applicableApiDescriptions, swaggerDoc, schemaRepository); }