@@ -17,6 +17,7 @@ internal class ConfigureSwaggerGeneratorOptions : IConfigureOptions<SwaggerGener
17
17
private readonly SwaggerGenOptions _swaggerGenOptions ;
18
18
private readonly IServiceProvider _serviceProvider ;
19
19
private readonly IWebHostEnvironment _hostingEnv ;
20
+ private readonly Dictionary < Type , object > _filterInstances = [ ] ;
20
21
21
22
public ConfigureSwaggerGeneratorOptions (
22
23
IOptions < SwaggerGenOptions > swaggerGenOptionsAccessor ,
@@ -33,7 +34,7 @@ public void Configure(SwaggerGeneratorOptions options)
33
34
DeepCopy ( _swaggerGenOptions . SwaggerGeneratorOptions , options ) ;
34
35
35
36
// Create and add any filters that were specified through the FilterDescriptor lists ...
36
-
37
+ // if a filter implements both interfaces, the same instance will be used
37
38
foreach ( var filterDescriptor in _swaggerGenOptions . ParameterFilterDescriptors )
38
39
{
39
40
if ( filterDescriptor . Type . IsAssignableTo ( typeof ( IParameterFilter ) ) )
@@ -120,8 +121,16 @@ public void DeepCopy(SwaggerGeneratorOptions source, SwaggerGeneratorOptions tar
120
121
121
122
private TFilter GetOrCreateFilter < TFilter > ( FilterDescriptor filterDescriptor )
122
123
{
123
- return ( TFilter ) ( filterDescriptor . FilterInstance
124
+ if ( _filterInstances . TryGetValue ( filterDescriptor . Type , out var value ) )
125
+ {
126
+ return ( TFilter ) value ;
127
+ }
128
+
129
+ var instance = ( TFilter ) ( filterDescriptor . FilterInstance
124
130
?? ActivatorUtilities . CreateInstance ( _serviceProvider , filterDescriptor . Type , filterDescriptor . Arguments ) ) ;
131
+
132
+ _filterInstances . Add ( filterDescriptor . Type , instance ) ;
133
+ return instance ;
125
134
}
126
135
}
127
136
}
0 commit comments