Skip to content

Commit ddd0627

Browse files
Ensure document filters can affect inferred security schemes
1 parent 1332212 commit ddd0627

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs

+25-11
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,41 @@ public SwaggerGenerator(
4141

4242
public async Task<OpenApiDocument> GetSwaggerAsync(string documentName, string host = null, string basePath = null)
4343
{
44-
var (applicableApiDescriptions, swaggerDoc, schemaRepository) = GetSwaggerDocument(documentName, host, basePath);
44+
var (applicableApiDescriptions, swaggerDoc, schemaRepository) = GetSwaggerDocumentWithoutFilters(documentName, host, basePath);
45+
4546
swaggerDoc.Components.SecuritySchemes = await GetSecuritySchemes();
47+
48+
// NOTE: Filter processing moved here so they may effect generated security schemes
49+
var filterContext = new DocumentFilterContext(applicableApiDescriptions, _schemaGenerator, schemaRepository);
50+
foreach (var filter in _options.DocumentFilters)
51+
{
52+
filter.Apply(swaggerDoc, filterContext);
53+
}
54+
55+
swaggerDoc.Components.Schemas = new SortedDictionary<string, OpenApiSchema>(swaggerDoc.Components.Schemas, _options.SchemaComparer);
56+
4657
return swaggerDoc;
4758
}
4859

4960
public OpenApiDocument GetSwagger(string documentName, string host = null, string basePath = null)
5061
{
51-
var (applicableApiDescriptions, swaggerDoc, schemaRepository) = GetSwaggerDocument(documentName, host, basePath);
62+
var (applicableApiDescriptions, swaggerDoc, schemaRepository) = GetSwaggerDocumentWithoutFilters(documentName, host, basePath);
63+
5264
swaggerDoc.Components.SecuritySchemes = GetSecuritySchemes().Result;
65+
66+
// NOTE: Filter processing moved here so they may effect generated security schemes
67+
var filterContext = new DocumentFilterContext(applicableApiDescriptions, _schemaGenerator, schemaRepository);
68+
foreach (var filter in _options.DocumentFilters)
69+
{
70+
filter.Apply(swaggerDoc, filterContext);
71+
}
72+
73+
swaggerDoc.Components.Schemas = new SortedDictionary<string, OpenApiSchema>(swaggerDoc.Components.Schemas, _options.SchemaComparer);
74+
5375
return swaggerDoc;
5476
}
5577

56-
private (IEnumerable<ApiDescription>, OpenApiDocument, SchemaRepository) GetSwaggerDocument(string documentName, string host = null, string basePath = null)
78+
private (IEnumerable<ApiDescription>, OpenApiDocument, SchemaRepository) GetSwaggerDocumentWithoutFilters(string documentName, string host = null, string basePath = null)
5779
{
5880
if (!_options.SwaggerDocs.TryGetValue(documentName, out OpenApiInfo info))
5981
throw new UnknownSwaggerDocument(documentName, _options.SwaggerDocs.Select(d => d.Key));
@@ -77,14 +99,6 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin
7799
SecurityRequirements = new List<OpenApiSecurityRequirement>(_options.SecurityRequirements)
78100
};
79101

80-
var filterContext = new DocumentFilterContext(applicableApiDescriptions, _schemaGenerator, schemaRepository);
81-
foreach (var filter in _options.DocumentFilters)
82-
{
83-
filter.Apply(swaggerDoc, filterContext);
84-
}
85-
86-
swaggerDoc.Components.Schemas = new SortedDictionary<string, OpenApiSchema>(swaggerDoc.Components.Schemas, _options.SchemaComparer);
87-
88102
return (applicableApiDescriptions, swaggerDoc, schemaRepository);
89103
}
90104

0 commit comments

Comments
 (0)