Swagger上传FileFilter for 5.0

问题描述 投票:0回答:1

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

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

但是,由于我迁移到了net core 3.0,所以我需要将大手笔更新为5.0。我的FileFilter需要修改,但是我无法成功迁移文件过滤器。有人可以帮忙吗?

svagger 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一起使用。

感谢您的协助。

swagger swagger-ui .net-core-3.0
1个回答
0
投票

5.0-rc4应该与没有自定义操作过滤器的文件一起使用-如果有[FromForm]参数属性,则只需删除。

GitHub issue discussion中指定并进行了自我测试。

© www.soinside.com 2019 - 2024. All rights reserved.