我在这里找到了一种方法来实现这个目标:
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。