用于 IFormFile 的 Asp.Net Core swagger 帮助页面 [英] Asp.Net Core swagger Help Pages for IFormFile

查看:11
本文介绍了用于 IFormFile 的 Asp.Net Core swagger 帮助页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to setup swagger for testing models which have IFormFile properties. For example I have next api method

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

还有我的参数模型

public class TestPostFileArgs
{
    public int UserId { get; set; }
    public int? CompanyId { get; set; }
    public IFormFile CompanyFile { get; set; }
}

默认 swagger 生成不允许测试的帮助页面为了解决这个问题,我写了下一个 OperationFilter

By default swagger generate help page which does not allow to test it To solve this problem I wrote next 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" };
    }
}

在这之后,一切都按照我对 swagger 的期望工作.

And after this everething works as I expect from swagger.

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

For now this solution works for me, but it feels not right. Maybe I am missing some simple solution for this. Also this approach does not handle List or complex object properties with IFormFile or maybe something else.

推荐答案

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

For you ASP.NET Core developers, there's an issue written up in the Swashbuckle.AspNetCore GitHub repo for this: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/193. It has some working code for an OperationFilter in the comments as well -- that one worked better for me than the other ones in this question.

这篇关于用于 IFormFile 的 Asp.Net Core swagger 帮助页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆