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

Added dependency injection/easy registration for async filters #3030

Merged
merged 10 commits into from
Aug 27, 2024
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<UseArtifactsOutput>true</UseArtifactsOutput>
<VersionPrefix>6.7.4</VersionPrefix>
<VersionPrefix>6.8.0</VersionPrefix>
<WarnOnPackingNonPackableProject>false</WarnOnPackingNonPackableProject>
</PropertyGroup>
<PropertyGroup Condition=" '$(GITHUB_ACTIONS)' != '' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,57 @@ public void Configure(SwaggerGeneratorOptions options)

// Create and add any filters that were specified through the FilterDescriptor lists ...

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

if (filterDescriptor.Type.IsAssignableTo(typeof(IParameterAsyncFilter)))
{
options.ParameterAsyncFilters.Add(GetOrCreateFilter<IParameterAsyncFilter>(filterDescriptor));
}
martincostello marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

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

_swaggerGenOptions.RequestBodyFilterDescriptors.ForEach(
filterDescriptor => options.RequestBodyFilters.Add(GetOrCreateFilter<IRequestBodyFilter>(filterDescriptor)));
if (filterDescriptor.Type.IsAssignableTo(typeof(IOperationAsyncFilter)))
{
options.OperationAsyncFilters.Add(GetOrCreateFilter<IOperationAsyncFilter>(filterDescriptor));
}
}

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

_swaggerGenOptions.DocumentFilterDescriptors.ForEach(
filterDescriptor => options.DocumentFilters.Add(GetOrCreateFilter<IDocumentFilter>(filterDescriptor)));
if (filterDescriptor.Type.IsAssignableTo(typeof(IDocumentAsyncFilter)))
{
options.DocumentAsyncFilters.Add(GetOrCreateFilter<IDocumentAsyncFilter>(filterDescriptor));
}
}

if (!options.SwaggerDocs.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ public static void AddSchemaFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.SchemaFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand All @@ -393,6 +394,25 @@ public static void ParameterFilter<TFilter>(
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Parameters asynchronously after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from <see cref="IParameterAsyncFilter"/></typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="arguments">Optionally inject parameters through filter constructors</param>
public static void ParameterAsyncFilter<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
params object[] arguments)
where TFilter : IParameterAsyncFilter
{
if (swaggerGenOptions == null) throw new ArgumentNullException(nameof(swaggerGenOptions));
swaggerGenOptions.ParameterFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
Arguments = arguments
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Parameters after they're initially generated
/// </summary>
Expand All @@ -408,6 +428,27 @@ public static void AddParameterFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.ParameterFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Parameters asynchronously after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from <see cref="IParameterAsyncFilter"/></typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="filterInstance">The filter instance to use.</param>
public static void AddParameterAsyncFilterInstance<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
TFilter filterInstance)
where TFilter : IParameterAsyncFilter
{
if (swaggerGenOptions == null) throw new ArgumentNullException(nameof(swaggerGenOptions));
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.ParameterFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand All @@ -431,6 +472,25 @@ public static void RequestBodyFilter<TFilter>(
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify RequestBodys asynchronously after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from <see cref="IRequestBodyAsyncFilter"/></typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="arguments">Optionally inject parameters through filter constructors</param>
public static void RequestBodyAsyncFilter<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
params object[] arguments)
where TFilter : IRequestBodyAsyncFilter
{
if (swaggerGenOptions == null) throw new ArgumentNullException(nameof(swaggerGenOptions));
swaggerGenOptions.RequestBodyFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
Arguments = arguments
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify RequestBodys after they're initially generated
/// </summary>
Expand All @@ -446,6 +506,27 @@ public static void AddRequestBodyFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.RequestBodyFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify RequestBodys asynchronously after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from <see cref="IRequestBodyAsyncFilter"/></typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="filterInstance">The filter instance to use.</param>
public static void AddRequestBodyAsyncFilterInstance<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
TFilter filterInstance)
where TFilter : IRequestBodyAsyncFilter
{
if (swaggerGenOptions == null) throw new ArgumentNullException(nameof(swaggerGenOptions));
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.RequestBodyFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand All @@ -469,6 +550,25 @@ public static void OperationFilter<TFilter>(
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Operations asynchronously after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from <see cref="IOperationAsyncFilter"/></typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="arguments">Optionally inject parameters through filter constructors</param>
public static void OperationAsyncFilter<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
params object[] arguments)
where TFilter : IOperationAsyncFilter
{
if (swaggerGenOptions == null) throw new ArgumentNullException(nameof(swaggerGenOptions));
swaggerGenOptions.OperationFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
Arguments = arguments
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Operations after they're initially generated
/// </summary>
Expand All @@ -484,6 +584,27 @@ public static void AddOperationFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.OperationFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify Operations asynchronously after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from <see cref="IOperationAsyncFilter"/></typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="filterInstance">The filter instance to use</param>
public static void AddOperationAsyncFilterInstance<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
TFilter filterInstance)
where TFilter : IOperationAsyncFilter
{
if (swaggerGenOptions == null) throw new ArgumentNullException(nameof(swaggerGenOptions));
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.OperationFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand All @@ -507,6 +628,26 @@ public static void DocumentFilter<TFilter>(
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify SwaggerDocuments asynchronously after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from <see cref="IDocumentAsyncFilter"/></typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="arguments">Optionally inject parameters through filter constructors</param>
/// <exception cref="ArgumentNullException"></exception>
public static void DocumentAsyncFilter<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
params object[] arguments)
where TFilter : IDocumentAsyncFilter
{
if (swaggerGenOptions == null) throw new ArgumentNullException(nameof(swaggerGenOptions));
swaggerGenOptions.DocumentFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
Arguments = arguments
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify SwaggerDocuments after they're initially generated
/// </summary>
Expand All @@ -522,6 +663,28 @@ public static void AddDocumentFilterInstance<TFilter>(
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.DocumentFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}

/// <summary>
/// Extend the Swagger Generator with "filters" that can modify SwaggerDocuments asynchronously after they're initially generated
/// </summary>
/// <typeparam name="TFilter">A type that derives from <see cref="IDocumentAsyncFilter"/></typeparam>
/// <param name="swaggerGenOptions"></param>
/// <param name="filterInstance">The filter instance to use.</param>
/// <exception cref="ArgumentNullException"></exception>
public static void AddDocumentAsyncFilterInstance<TFilter>(
this SwaggerGenOptions swaggerGenOptions,
TFilter filterInstance)
where TFilter : IDocumentAsyncFilter
{
if (swaggerGenOptions == null) throw new ArgumentNullException(nameof(swaggerGenOptions));
if (filterInstance == null) throw new ArgumentNullException(nameof(filterInstance));
swaggerGenOptions.DocumentFilterDescriptors.Add(new FilterDescriptor
{
Type = typeof(TFilter),
FilterInstance = filterInstance
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
static Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.AddDocumentAsyncFilterInstance<TFilter>(this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions swaggerGenOptions, TFilter filterInstance) -> void
static Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.AddOperationAsyncFilterInstance<TFilter>(this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions swaggerGenOptions, TFilter filterInstance) -> void
static Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.AddParameterAsyncFilterInstance<TFilter>(this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions swaggerGenOptions, TFilter filterInstance) -> void
static Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.AddRequestBodyAsyncFilterInstance<TFilter>(this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions swaggerGenOptions, TFilter filterInstance) -> void
static Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.DocumentAsyncFilter<TFilter>(this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions swaggerGenOptions, params object[] arguments) -> void
static Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.OperationAsyncFilter<TFilter>(this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions swaggerGenOptions, params object[] arguments) -> void
static Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.ParameterAsyncFilter<TFilter>(this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions swaggerGenOptions, params object[] arguments) -> void
static Microsoft.Extensions.DependencyInjection.SwaggerGenOptionsExtensions.RequestBodyAsyncFilter<TFilter>(this Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions swaggerGenOptions, params object[] arguments) -> void
Loading