gpt4 book ai didi

c# - Swagger 5.0 - IDocumentFilter(SwaggerDocument 到 OpenApiDocument)自定义 "Definitions"

转载 作者:行者123 更新时间:2023-12-04 02:39:07 25 4
gpt4 key购买 nike

我们正在将应用程序从 .NET Core 2.1 迁移到 3.1,我们需要升级 从 4.X Swagger 到 5.X 以及。使用 swagger 4.x,我们能够删除某些用特定属性标记的属性,如下所示:

public class SwaggerIgnoreFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
var allTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(i => i.GetTypes()).ToList();

foreach (var definition in swaggerDoc?.Definitions)
{
var type = allTypes.FirstOrDefault(x => x.Name == definition.Key);
if (type != null)
{
var properties = type.GetProperties();
foreach (var prop in properties.ToList())
{
var ignoreAttribute = prop.GetCustomAttribute(typeof(SwaggerIgnoreAttribute), false);

if (ignoreAttribute != null)
{
definition.Value.Properties.Remove(prop.Name);
}
}
}
}
}
}

问题是,在 swagger 5.0 中,IDocumentFilter 发生了变化,我们用 OpenApiDocument 代替了 SwaggerDocument,后者已经带走了 定义 属性,我们无法弄清楚如何从 swagger 页面隐藏某些属性。任何帮助/建议/链接都会很棒。谢谢。

最佳答案

现在您可以使用 swaggerDoc.Components.Schemas

public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
var allTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(i => i.GetTypes()).ToList();

foreach (var definition in swaggerDoc.Components.Schemas)
{
var type = allTypes.FirstOrDefault(x => x.Name == definition.Key);
if (type != null)
{
var properties = type.GetProperties();
foreach (var prop in properties.ToList())
{
var ignoreAttribute = prop.GetCustomAttribute(typeof(SwaggerIgnoreAttribute), false);

if (ignoreAttribute != null)
{
definition.Value.Properties.Remove(prop.Name);
}
}
}
}
}

关于c# - Swagger 5.0 - IDocumentFilter(SwaggerDocument 到 OpenApiDocument)自定义 "Definitions",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60207548/

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