Skip to content

Commit

Permalink
Refactor filter descriptor type checks in SwaggerGen (#3125)
Browse files Browse the repository at this point in the history
Introduce `IsAssignableTo` method in `FilterDescriptor` class to check if `FilterInstance` is an instance of a specified type or if the `Type` property is assignable to the specified type.
  • Loading branch information
iskandersierra authored Nov 4, 2024
1 parent e1c5153 commit 0554326
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,51 +36,51 @@ public void Configure(SwaggerGeneratorOptions options)

foreach (var filterDescriptor in _swaggerGenOptions.ParameterFilterDescriptors)
{
if (filterDescriptor.Type.IsAssignableTo(typeof(IParameterFilter)))
if (filterDescriptor.IsAssignableTo(typeof(IParameterFilter)))
{
options.ParameterFilters.Add(GetOrCreateFilter<IParameterFilter>(filterDescriptor));
}

if (filterDescriptor.Type.IsAssignableTo(typeof(IParameterAsyncFilter)))
if (filterDescriptor.IsAssignableTo(typeof(IParameterAsyncFilter)))
{
options.ParameterAsyncFilters.Add(GetOrCreateFilter<IParameterAsyncFilter>(filterDescriptor));
}
}

foreach (var filterDescriptor in _swaggerGenOptions.RequestBodyFilterDescriptors)
{
if (filterDescriptor.Type.IsAssignableTo(typeof(IRequestBodyFilter)))
if (filterDescriptor.IsAssignableTo(typeof(IRequestBodyFilter)))
{
options.RequestBodyFilters.Add(GetOrCreateFilter<IRequestBodyFilter>(filterDescriptor));
}

if (filterDescriptor.Type.IsAssignableTo(typeof(IRequestBodyAsyncFilter)))
if (filterDescriptor.IsAssignableTo(typeof(IRequestBodyAsyncFilter)))
{
options.RequestBodyAsyncFilters.Add(GetOrCreateFilter<IRequestBodyAsyncFilter>(filterDescriptor));
}
}

foreach (var filterDescriptor in _swaggerGenOptions.OperationFilterDescriptors)
{
if (filterDescriptor.Type.IsAssignableTo(typeof(IOperationFilter)))
if (filterDescriptor.IsAssignableTo(typeof(IOperationFilter)))
{
options.OperationFilters.Add(GetOrCreateFilter<IOperationFilter>(filterDescriptor));
}

if (filterDescriptor.Type.IsAssignableTo(typeof(IOperationAsyncFilter)))
if (filterDescriptor.IsAssignableTo(typeof(IOperationAsyncFilter)))
{
options.OperationAsyncFilters.Add(GetOrCreateFilter<IOperationAsyncFilter>(filterDescriptor));
}
}

foreach (var filterDescriptor in _swaggerGenOptions.DocumentFilterDescriptors)
{
if (filterDescriptor.Type.IsAssignableTo(typeof(IDocumentFilter)))
if (filterDescriptor.IsAssignableTo(typeof(IDocumentFilter)))
{
options.DocumentFilters.Add(GetOrCreateFilter<IDocumentFilter>(filterDescriptor));
}

if (filterDescriptor.Type.IsAssignableTo(typeof(IDocumentAsyncFilter)))
if (filterDescriptor.IsAssignableTo(typeof(IDocumentAsyncFilter)))
{
options.DocumentAsyncFilters.Add(GetOrCreateFilter<IDocumentAsyncFilter>(filterDescriptor));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@ public class FilterDescriptor
public object[] Arguments { get; set; }

public object FilterInstance { get; set; }

internal bool IsAssignableTo(Type type)
{
return (FilterInstance != null && type.IsInstanceOfType(FilterInstance)) ||
(Type != null && Type.IsAssignableTo(type));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,31 @@ public static void AddingDocumentAsyncFilterTypes_WhenConfiguringOptions_Differe
Assert.Equal(2, swaggerGeneratorOptions.DocumentAsyncFilters.Count);
Assert.NotSame(swaggerGeneratorOptions.DocumentAsyncFilters.First(), swaggerGeneratorOptions.DocumentAsyncFilters.Last());
}

[Fact]
public static void AddingFilterDescriptorWithFilterInstance_WhenConfiguringOptions_NoExceptionIsThrown()
{
var webhostingEnvironment = Substitute.For<IWebHostEnvironment>();
webhostingEnvironment.ApplicationName.Returns("Swashbuckle.AspNetCore.SwaggerGen.Test");

var options = new SwaggerGenOptions();
options.OperationFilterDescriptors.Add(
new FilterDescriptor()
{
Type = null,
FilterInstance = new TestOperationFilter(),
});

using var serviceProvider = new ServiceCollection().BuildServiceProvider();

var configureSwaggerGeneratorOptions = new ConfigureSwaggerGeneratorOptions(
Options.Create(options),
serviceProvider,
webhostingEnvironment);
var swaggerGeneratorOptions = new SwaggerGeneratorOptions();

configureSwaggerGeneratorOptions.Configure(swaggerGeneratorOptions);

Assert.Single(swaggerGeneratorOptions.OperationFilters);
}
}

0 comments on commit 0554326

Please sign in to comment.