【发布时间】:2017-04-21 16:53:51
【问题描述】:
我正在使用 Swashbuckle 为 webapi2 项目生成 swagger 文档\UI。我们的模型与一些遗留接口共享,因此我想忽略模型上的几个属性。我不能使用 JsonIgnore 属性,因为遗留接口也需要序列化为 JSON,所以我不想全局忽略属性,只是在 Swashbuckle 配置中。
我在这里找到了一种记录方法:
https://github.com/domaindrivendev/Swashbuckle/issues/73
但这似乎与当前的 Swashbuckle 版本已过时。
旧版 Swashbuckle 推荐的方法是使用 IModelFilter 实现,如下所示:
public class OmitIgnoredProperties : IModelFilter
{
public void Apply(DataType model, DataTypeRegistry dataTypeRegistry, Type type)
{
var ignoredProperties = … // use reflection to find any properties on
// type decorated with the ignore attributes
foreach (var prop in ignoredProperties)
model.Properties.Remove(prop.Name);
}
}
SwaggerSpecConfig.Customize(c => c.ModelFilter<OmitIgnoredProperties>());
但我不确定如何配置 Swashbuckle 以在当前版本中使用 IModelFilter?我正在使用 Swashbuckle 5.5.3。
【问题讨论】:
-
您实际上可以使用 JsonIgnore 属性,它不会在招摇中显示该属性
-
如问题中所述,我不想使用 JsonIgnore,因为我有也需要使用模型的遗留代码,如果我应用 JsonIgnore 会影响招摇和遗留代码......
标签: c# asp.net-web-api swagger swashbuckle