Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor filter descriptor type checks in SwaggerGen #3125

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
iskandersierra marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ public static void AddSchemaFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.SchemaFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
martincostello marked this conversation as resolved.
Show resolved Hide resolved
FilterInstance = filterInstance
});
}
Expand Down Expand Up @@ -428,7 +427,6 @@ public static void AddParameterFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.ParameterFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand All @@ -448,7 +446,6 @@ public static void AddParameterAsyncFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.ParameterFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand Down Expand Up @@ -506,7 +503,6 @@ public static void AddRequestBodyFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.RequestBodyFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand All @@ -526,7 +522,6 @@ public static void AddRequestBodyAsyncFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.RequestBodyFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand Down Expand Up @@ -584,7 +579,6 @@ public static void AddOperationFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.OperationFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand All @@ -604,7 +598,6 @@ public static void AddOperationAsyncFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.OperationFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand Down Expand Up @@ -663,7 +656,6 @@ public static void AddDocumentFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.DocumentFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand All @@ -684,7 +676,6 @@ public static void AddDocumentAsyncFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.DocumentFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand Down