5.0 的 Swagger 上传文件过滤器

Swagger Upload FileFilter for 5.0

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

我目前有一个 Swashbuckle.AspNetCore 4.0.1: https://www.nuget.org/packages/Swashbuckle.AspNetCore/4.0.1?_src=template

但是自从我迁移到 net core 3.0 后,我需要将我的 swagger 更新到 5.0。我的 FileFilter 需要修改,但是我未能成功迁移文件过滤器。有人可以帮忙吗?

当前用于 swagger 4.0.1 的 SwaggerUploadFileFilter:

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"
            });
        }
    }
}

尝试使用 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; }
    }
}

我无法使 5.0 版本与 IFormFile 一起使用。

如有任何帮助,我们将不胜感激。

5.0-rc4 应该可以在没有自定义操作过滤器的情况下使用文件 - 如果有参数属性,您只需删除 [FromForm] 参数属性。

GitHub issue discussion中指定并亲自测试。