gpt4 book ai didi

c# - Swagger 上传文件过滤器 5.0

转载 作者:行者123 更新时间:2023-12-02 17:18:40 24 4
gpt4 key购买 nike

有人成功为 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 需要修改,但我未能成功迁移文件过滤器。有人可以帮忙吗?

当前正在工作的 swagger4.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 中指定并测试了自己。

关于c# - Swagger 上传文件过滤器 5.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58768489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com