C# Swagger上传文件过滤器5.0版

C# Swagger上传文件过滤器5.0版,c#,swagger,swagger-ui,.net-core-3.0,C#,Swagger,Swagger Ui,.net Core 3.0,有人成功地为swagger 5.0构建了文件过滤器吗 我目前为Swashback.AspNetCore 4.0.1提供了一个: 然而,自从我迁移到NetCore3.0之后,我需要将我的swagger更新到5.0。我的文件筛选器需要重新设置,但是我未能成功迁移文件筛选器。有人能帮忙吗 swagger 4.0.1的当前工作swagger UploadFileFilter: public class SwaggerUploadFileFilter : IOperationFilter { pr

有人成功地为swagger 5.0构建了文件过滤器吗

我目前为Swashback.AspNetCore 4.0.1提供了一个:

然而,自从我迁移到NetCore3.0之后,我需要将我的swagger更新到5.0。我的文件筛选器需要重新设置,但是我未能成功迁移文件筛选器。有人能帮忙吗

swagger 4.0.1的当前工作swagger UploadFileFilter:

public class SwaggerUploadFileFilter : IOperationFilter
{
    private const string formDataMimeType = "multipart/form-data";
    private static readonly string[] formFilePropertyNames =
        typeof(IFormFile).GetTypeInfo().DeclaredProperties.Select(p => p.Name).ToArray();

    public void Apply(Operation operation, OperationFilterContext context)
    {
        var parameters = operation.Parameters;
        if (parameters == null || parameters.Count == 0) return;

        var formFileParameterNames = new List<string>();
        var formFileSubParameterNames = new List<string>();

        foreach (var actionParameter in context.ApiDescription.ActionDescriptor.Parameters)
        {
            var properties =
                actionParameter.ParameterType.GetProperties()
                    .Where(p => p.PropertyType == typeof(IFormFile))
                    .Select(p => p.Name)
                    .ToArray();

            if (properties.Length != 0)
            {
                formFileParameterNames.AddRange(properties);
                formFileSubParameterNames.AddRange(properties);
                continue;
            }

            if (actionParameter.ParameterType != typeof(IFormFile)) continue;
            formFileParameterNames.Add(actionParameter.Name);
        }

        if (!formFileParameterNames.Any()) return;

        var consumes = operation.Consumes;
        consumes.Clear();
        consumes.Add(formDataMimeType);

        foreach (var parameter in parameters.ToArray())
        {
            if (!(parameter is NonBodyParameter) || parameter.In != "formData") continue;

            if (formFileSubParameterNames.Any(p => parameter.Name.StartsWith(p + "."))
                || formFilePropertyNames.Contains(parameter.Name))
                parameters.Remove(parameter);
        }

        foreach (var formFileParameter in formFileParameterNames)
        {
            parameters.Add(new NonBodyParameter()
            {
                Name = formFileParameter,
                Type = "file",
                In = "formData"
            });
        }
    }
}
公共类SwiggerUploadFileFilter:IOperationFilter
{
private const string formDataMimeType=“多部分/表单数据”;
私有静态只读字符串[]formFilePropertyNames=
typeof(ifformfile).GetTypeInfo().DeclaredProperties.Select(p=>p.Name).ToArray();
公共无效应用(操作,操作筛选器上下文)
{
var参数=操作参数;
if(parameters==null | | parameters.Count==0)返回;
var formFileParameterNames=新列表();
var formFileSubParameterNames=new List();
foreach(context.apiscription.ActionDescriptor.Parameters中的var actionParameter)
{
var特性=
actionParameter.ParameterType.GetProperties()
.Where(p=>p.PropertyType==typeof(IFormFile))
.Select(p=>p.Name)
.ToArray();
如果(properties.Length!=0)
{
formFileParameterNames.AddRange(属性);
formFileSubParameterNames.AddRange(属性);
继续;
}
如果(actionParameter.ParameterType!=typeof(IFormFile))继续;
formFileParameterNames.Add(actionParameter.Name);
}
如果(!formFileParameterNames.Any())返回;
var consumes=operation.consumes;
消耗。清除();
consumes.Add(formDataMimeType);
foreach(parameters.ToArray()中的var参数)
{
如果(!(参数为非bodyParameter)| parameter.In!=“formData”)继续;
if(formFileSubParameterNames.Any(p=>parameter.Name.StartsWith(p+)))
||formFilePropertyNames.Contains(parameter.Name))
参数。删除(参数);
}
foreach(formFileParameterNames中的var formFileParameter)
{
parameters.Add(新的NonBodyParameter()
{
Name=formFileParameter,
Type=“file”,
In=“formData”
});
}
}
}
尝试使用swagger 5.0的联机代码:

public class SwaggerUploadFileFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (!(operation?.RequestBody?.Content?.Any(x => x.Key.ToLower() == "multipart/form-data") ?? false)) return;

        var uploadFiles = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
            .Union(context.MethodInfo.GetCustomAttributes(true))
            .OfType<SwaggerUploadFile>();

        if (uploadFiles.Count() == 0) return;

        var uploadFile = uploadFiles.First();

        operation.RequestBody.Content["multipart/form-data"].Schema.Properties =
            new Dictionary<string, OpenApiSchema>
            {
                [uploadFile.Parameter] = new OpenApiSchema
                {
                    Type = "string",
                    Format = "binary",
                    Description = uploadFile.Description
                }
            };

        if (!string.IsNullOrEmpty(uploadFile.Example))
        {
            operation.RequestBody.Content["multipart/form-data"].Schema.Example = new OpenApiString(uploadFile.Example);
            operation.RequestBody.Content["multipart/form-data"].Schema.Description = uploadFile.Example;
        }
    }

    private class SwaggerUploadFile
    {
        public string Parameter { get; set; }

        public string Description { get; set; }

        public string Example { get; set; }
    }
}
公共类SwiggerUploadFileFilter:IOperationFilter
{
公共无效应用(OpenApiOperation操作,OperationFilterContext上下文)
{
if(!(操作?.RequestBody?.Content?.Any(x=>x.Key.ToLower()==“多部分/表单数据”)?false))返回;
var uploadFiles=context.MethodInfo.DeclaringType.GetCustomAttributes(true)
.Union(context.MethodInfo.GetCustomAttributes(true))
.of type();
if(uploadFiles.Count()==0)返回;
var uploadFile=uploadFiles.First();
operation.RequestBody.Content[“多部分/表单数据”].Schema.Properties=
新词典
{
[uploadFile.Parameter]=新的OpenApiSchema
{
Type=“string”,
Format=“binary”,
Description=上传文件。Description
}
};
如果(!string.IsNullOrEmpty(uploadFile.Example))
{
operation.RequestBody.Content[“multipart/form data”].Schema.Example=new OpenApiString(uploadFile.Example);
operation.RequestBody.Content[“multipart/form data”].Schema.Description=uploadFile.Example;
}
}
私有类SwaggerUploadFile
{
公共字符串参数{get;set;}
公共字符串说明{get;set;}
公共字符串示例{get;set;}
}
}
我在使5.0版本与IFormFile一起工作时遇到问题


感谢您的帮助。

5.0-rc4应该可以处理没有自定义操作筛选器的文件-如果您有参数属性,只需删除
[FromForm]
即可


在中指定并测试我自己。

似乎做到了这一点!非常感谢你发现这一点。我感谢你花时间写下这个答案。干杯,伙计!如果参数仅为ifformfile,则它可以工作;如果其ifformfile数组(ifformfile[]),则它不工作。您是否在阵列中遇到此问题?@dsr301,
IFormFileCollection
在这种情况下应该可以工作。您还可以查看他们的示例。