用于IFormFile的Asp.Net Core swagger帮助页面

Yur*_*iyP 6 asp.net-web-api swagger asp.net-core

我试图设置swagger来测试具有IFormFile属性的模型.例如,我有下一个api方法

[HttpPost]
public ApiResult<UserModel> SaveTestFileData([FromForm]TestPostFileArgs args)
{
    var result = new UserModel() { Id = 1, Name = $"SaveTestFileData {args.UserId} company: {args.CompanyId}, file length: {args.CompanyFile.Length}" };
    return ApiResult.Success(result);
}
Run Code Online (Sandbox Code Playgroud)

和我的参数模型

public class TestPostFileArgs
{
    public int UserId { get; set; }
    public int? CompanyId { get; set; }
    public IFormFile CompanyFile { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,swagger生成帮助页面,不允许对其进行测试 错误的招摇ui 为了解决这个问题,我写了下一个OperationFilter

public class FormFileOperationFilter: IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        if (operation.Parameters == null)
            return;

        var fileParamNames = context.ApiDescription.ActionDescriptor.Parameters
            .SelectMany(x => x.ParameterType.GetProperties())
            .Where(x => x.PropertyType.IsAssignableFrom(typeof (IFormFile)))
            .Select(x => x.Name)
            .ToList();
        if (!fileParamNames.Any())
            return;

        var paramsToRemove = new List<IParameter>();
        foreach (var param in operation.Parameters)
        {
            paramsToRemove.AddRange(from fileParamName in fileParamNames where param.Name.StartsWith(fileParamName + ".") select param);
        }
        paramsToRemove.ForEach(x => operation.Parameters.Remove(x));
        foreach (var paramName in fileParamNames)
        {
            var fileParam = new NonBodyParameter
                {
                    Type = "file",
                    Name = paramName,
                    In = "formData"
                };
            operation.Parameters.Add(fileParam);
        }
        foreach (IParameter param in operation.Parameters)
        {
            param.In = "formData";
        }

        operation.Consumes = new List<string>() { "multipart/form-data" };
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况发生之后,正如我所期待的那样. 正确的招摇ui

目前这个解决方案适合我,但感觉不对.也许我错过了一些简单的解决方案.此方法也不使用IFormFile或其他方法处理List或复杂对象属性.

IGx*_*x89 5

对于 ASP.NET Core 开发人员,Swashbuckle.AspNetCore GitHub 存储库中为此写了一个问题: https: //github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/193。它在评论中也有一些用于操作过滤器的工作代码——其中一个对我来说比这个问题中的其他代码更有效。


Jim*_*non 2

我有同样的问题,你的解决方案对我有帮助。

我刚刚更改了OperationFilter,因为我的IFormFile 参数不是嵌套参数,而是操作方法的主要参数。

public class AddFileUploadParams : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        if (operation.Parameters == null)
            return;

        var formFileParams = context.ApiDescription.ActionDescriptor.Parameters
                                .Where(x => x.ParameterType.IsAssignableFrom(typeof(IFormFile)))
                                .Select(x => x.Name)
                                .ToList(); ;

        var formFileSubParams = context.ApiDescription.ActionDescriptor.Parameters
            .SelectMany(x => x.ParameterType.GetProperties())
            .Where(x => x.PropertyType.IsAssignableFrom(typeof(IFormFile)))
            .Select(x => x.Name)
            .ToList();

        var allFileParamNames = formFileParams.Union(formFileSubParams);

        if (!allFileParamNames.Any())
            return;

        var paramsToRemove = new List<IParameter>();
        foreach (var param in operation.Parameters)
        {
            paramsToRemove.AddRange(from fileParamName in allFileParamNames where param.Name.StartsWith(fileParamName + ".") select param);
        }
        paramsToRemove.ForEach(x => operation.Parameters.Remove(x));
        foreach (var paramName in allFileParamNames)
        {
            var fileParam = new NonBodyParameter
            {
                Type = "file",
                Name = paramName,
                In = "formData"
            };
            operation.Parameters.Add(fileParam);
        }
        foreach (IParameter param in operation.Parameters)
        {
            param.In = "formData";
        }

        operation.Consumes = new List<string>() { "multipart/form-data" };
    }
}
Run Code Online (Sandbox Code Playgroud)

我想您使用SwashBuckle来生成招摇。如果 swashBuckle 能处理这个问题就好了。如果我还有时间的话,也许我会做出贡献。