【发布时间】:2020-05-26 08:06:38
【问题描述】:
我正在尝试为 Swashbuckle 构建一个过滤器,以在 API 文档中省略项目的模型/实体/架构,保留控制器。采用的技术是 Swashbuckle.AspNetCore v3.0.0 / Swagger UI v3.17.1。我已经找到了在控制器中省略某个方法的方法,但我想省略文档中的模型。我发现了一个和我类似的问题,包括只隐藏属性。
跟随过滤器代码
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
有人有什么建议可以只隐藏文档中的模型/实体/模式,而不仅仅是它们的属性吗?如下图。
【问题讨论】:
标签: .net-core swagger swagger-ui swashbuckle