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); }