48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using System.Linq;
|
|
using Microsoft.OpenApi.Any;
|
|
using Microsoft.OpenApi.Models;
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
using xCommons.Configurations;
|
|
using xCommons.Extensions;
|
|
|
|
namespace xCommons.Filters
|
|
{
|
|
public class SwaggerDefaultValuesOperationFilter : IOperationFilter
|
|
{
|
|
private readonly XSwaggerConfiguration config;
|
|
|
|
public SwaggerDefaultValuesOperationFilter(
|
|
XSwaggerConfiguration config
|
|
)
|
|
{
|
|
this.config = config;
|
|
}
|
|
|
|
public void Apply(
|
|
OpenApiOperation operation,
|
|
OperationFilterContext context
|
|
)
|
|
{
|
|
//
|
|
// Validate Configuration ...
|
|
if (
|
|
!config.IsNull() &&
|
|
!config.DefaultValues.IsNull() &&
|
|
config.DefaultValues.HasChild()
|
|
)
|
|
{
|
|
//
|
|
foreach (var prv in config.DefaultValues)
|
|
{
|
|
//
|
|
// Detect Parameter ...
|
|
var swaggerParam = operation.Parameters.FirstOrDefault(p => p.Name == prv.Key);
|
|
if (!swaggerParam.IsNull())
|
|
{
|
|
swaggerParam.Schema.Default = new OpenApiString(prv.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |