Swashbuckle SchemaFilter for api 操作参数

Ris*_*o M 4 c# enums swagger swashbuckle asp.net-core

我已成功创建ISchemaFilter扩展swagger.json所描述的代码生成目的枚举属性定义在这里。这是我目前的SchemaFilter.Apply方法:

public void Apply(Schema schema, SchemaFilterContext context)
{
    if (context.SystemType.IsEnum)
    {
        var names = Enum.GetNames(context.SystemType);
        var values = Enum.GetValues(context.SystemType);
        var desc = "";

        foreach (var value in values)
        {
            var intValue = Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()));
            desc += $"{intValue}={value},";
        }
        desc = desc.TrimEnd(',');
        schema.Extensions.Add("x-enumNames", names);
        schema.Extensions["description"] = desc;
    }
}
Run Code Online (Sandbox Code Playgroud)

SchemaFilter在我的模型定义上正常工作,其中模型类具有枚举类型的成员。下面是输出示例:resolution-field,这是枚举类型,注意自定义x-enumNames和修改的description字段:

resolution: {
    format: "int32",
    enum: [
        1,
        2,
        3,
        4
    ],
    type: "integer",
    x-enumNames: [
        "Hour",
        "Day",
        "Month",
        "Year"
    ],
    description: "1=Hour,2=Day,3=Month,4=Year"
}
Run Code Online (Sandbox Code Playgroud)

问题是SchemaFilter不扩展操作参数中的枚举类型。例如下面的 api-method 有参数resolution

public async Task<ActionResult<ReturnType>> GetData(Models.ResolutionEnum resolution)
Run Code Online (Sandbox Code Playgroud)

这会为 swagger.json 生成以下操作参数定义(注意缺失x-EnumNames):

{
    name: "resolution",
    in: "query",
    required: true,
    type: "integer",
    format: "int32",
    enum: [
        1,
        2,
        3,
        4
    ]
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以扩展作为方法参数一部分的招摇枚举模式?

Ris*_*o M 5

感谢这个问题下的另一个答案,我发现Swashbuckle.AspNetCore.SwaggerGen命名空间中有多个扩展点。IParameterFilter正是我想要的,我能够注入x-enumNames方法参数定义。

以下是我制作的参数过滤器:

public class ModifyParametersFilter : IParameterFilter
{
    public void Apply(IParameter parameter, ParameterFilterContext context)
    {
        var type = context.ParameterInfo?.ParameterType;
        if (type == null)
            return;
        if (type.IsEnum)
        {
            var names = Enum.GetNames(type);
            var values = Enum.GetValues(type);
            var desc = "";

            foreach (var value in values)
            {
                var intValue = Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()));
                desc += $"{intValue}={value},";
            }
            desc = desc.TrimEnd(',');
            if (!parameter.Extensions.ContainsKey("x-enumNames"))
                parameter.Extensions.Add("x-enumNames", names);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

与其他过滤器一样,可以Startup.cs使用以下代码段激活它:

services.AddSwaggerGen(c =>
{
    ..
    c.ParameterFilter<ModifyParametersFilter>();
}
Run Code Online (Sandbox Code Playgroud)