如何配置Swashbuckle忽略模型上的属性
我正在使用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());
但我不确定如何配置Swashbuckle在当前版本中使用IModelFilter? 我正在使用Swashbuckle 5.5.3。
如果你需要这样做而不使用JsonIgnore(也许你仍然需要序列化/反序列化属性),那么只需创建一个自定义属性。
[AttributeUsage(AttributeTargets.Property)] public class SwaggerExcludeAttribute : Attribute { }
然后是类似于Johng的模式filter
public class SwaggerExcludeFilter : ISchemaFilter { #region ISchemaFilter Members public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type) { if (schema?.Properties == null || type == null) return; var excludedProperties = type.GetProperties() .Where(t => t.GetCustomAttribute() != null); foreach (var excludedProperty in excludedProperties) { if (schema.properties.ContainsKey(excludedProperty.Name)) schema.properties.Remove(excludedProperty.Name); } } #endregion }
不要忘记注册filter
c.SchemaFilter();
如果您将字段/属性标记为internal
或protected
或private
,swagbuckle会在swagger文档中自动忽略它。
好吧,有点戳,我找到了一种方法来使用ISchemaFilter:
public class ApplyCustomSchemaFilters : ISchemaFilter { public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type) { var excludeProperties = new[] {"myProp1", "myProp2", "myProp3"}; foreach(var prop in excludeProperties) if (schema.properties.ContainsKey(prop)) schema.properties.Remove(prop); } }
然后在调用httpConfiguration.EnableSwagger
我将httpConfiguration.EnableSwagger
设置为使用此SchemaFilter,如下所示:
c.SchemaFilter();
希望这有助于某人。 我仍然对是否有可能以某种方式使用IModelFilter感到好奇。
这是我与Newtonsoft.Json.JsonIgnoreAttribute一起使用的内容:
internal class ApplySchemaVendorExtensions : Swashbuckle.Swagger.ISchemaFilter { public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type) { foreach (var prop in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) .Where(p => p.GetCustomAttributes(typeof(Newtonsoft.Json.JsonIgnoreAttribute), true)?.Any() == true)) if (schema?.properties?.ContainsKey(prop.Name) == true) schema?.properties?.Remove(prop.Name); } }
( 基于互斥锁的答案 。)
我添加了另一行,没有NullReferenceException
问题。
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type) { var excludeProperties = new[] { "myProp1", "myProp2, myProp3"}; foreach (var prop in excludeProperties) if(schema.properties != null) // This line if (schema.properties.ContainsKey(prop)) schema.properties.Remove(prop); }
如果要删除所有模式
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type) { schema.properties = null; }
AspNetCore
解决方案如下所示:
public class SwaggerExcludeSchemaFilter : ISchemaFilter { public void Apply(Schema schema, SchemaFilterContext context) { if (schema?.Properties == null) { return; } var excludedProperties = context.SystemType.GetProperties().Where(t => t.GetCustomAttribute() != null); foreach (PropertyInfo excludedProperty in excludedProperties) { if (schema.Properties.ContainsKey(excludedProperty.Name)) { schema.Properties.Remove(excludedProperty.Name); } } } }
- 来自URI的Web API ModelBinding
- 更改我的服务器端WebAPI以获得async / await的好处
- Thread.CurrentPrincipal已经过身份validation,但ClaimsPrincipal.Current不是
- 使用媒体类型对ASP.NET Web API 2进行版本控制
- Web API中的MVC-6与MVC-5 BearerAuthentication
- SignalR hub中的简单Injector per-web-api-request依赖
- 将对象的数组传递给webapi
- 测试WebApi控制器Url.Link
- 使用HttpClient时如何在HttpContent中设置大字符串?