作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人成功为 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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!