gpt4 book ai didi

.net-core - 如何配置 Swashbuckle 以从文档中省略模板/实体/架构

转载 作者:行者123 更新时间:2023-12-03 22:00:12 24 4
gpt4 key购买 nike

我正在尝试为 Swashbuckle 构建一个过滤器,以在 API 文档中省略项目的模型/实体/架构,保留 Controller 。采用的技术是 Swashbuckle.AspNetCore v3.0.0/Swagger UI v3.17.1。我已经找到了在 Controller 中省略某个方法的方法,但我想省略文档中的模型。我发现了一个与我类似的问题,包括仅隐藏属性。

遵循过滤器代码

public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (!(context.ApiModel is ApiObject))
{
return;
}

var model = context as ApiObject;

if (schema?.Properties == null || model?.ApiProperties == null)
{
return;
}

var excludedProperties = model.Type
.GetProperties()
.Where(
t => t.GetCustomAttribute<SwaggerExcludeAttribute>() != null
);

var excludedSchemaProperties = model.ApiProperties
.Where(
ap => excludedProperties.Any(
pi => pi.Name == ap.MemberInfo.Name
)
);

foreach (var propertyToExclude in excludedSchemaProperties)
{
schema.Properties.Remove(propertyToExclude.ApiName);
}
}

报价: How to configure Swashbuckle to ignore property on model

有没有人有任何建议只隐藏文档中的模型/实体/模式,而不仅仅是它们的属性?如下图。

最佳答案

套装DefaultModelsExpandDepth在 Swashbuckle/Swagger UI 配置中设置为 -1:

app.UseSwaggerUI(c =>
{
...
c.DefaultModelsExpandDepth(-1);
}

关于.net-core - 如何配置 Swashbuckle 以从文档中省略模板/实体/架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60159998/

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