2016-12-06 37 views
3

忽略属性我使用Swashbuckle产生了webapi2项目招摇的文档\ UI。我们的模型与一些传统界面共享,因此我想在模型上忽略一些属性。我无法使用JsonIgnore属性,因为旧接口也需要序列化为JSON,因此我不想忽略全局属性,只需在Swashbuckle配置中。如何Swashbuckle配置对模型

我发现这样做在此处介绍的方法:

https://github.com/domaindrivendev/Swashbuckle/issues/73

但是这似乎是过时的当前Swashbuckle释放。

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的如下使用IModelFilter实现?我正在使用Swashbuckle 5.5.3。

+0

可以实际上使用JsonIgno欢迎使用属性重新将不招摇 –

+0

显示属性正如我不希望使用JsonIgnore因为我有遗留代码也需要使用模型中的问题中提到的,如果我申请JsonIgnore会影响招摇和遗留代码... – mutex

回答

4

好了,带着几分戳中我找到了一种方法来做到这一点使用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当我设置SwaggerDocsConfig使用此SchemaFilter如下:

c.SchemaFilter<ApplyCustomSchemaFilters>(); 

希望这可以帮助某人。尽管如此,我仍然对是否有可能使用IModelFilter感到好奇。

2

Based on mutex's answer

我添加另一条线路没有问题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;  
} 
3

这是我与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); 
    } 
} 
6

如果标记字段/属性为内部或受保护或私人的,它会在swagger文档中被swashbuckle自动忽略。

+0

这是最好的解决方案IMO – infl3x

1

如果你需要做到这一点,但没有使用JsonIgnore(也许你还需要序列化/反序列化属性),那么只需要创建一个自定义属性。

​​

然后类似Johng's架构过滤器

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<SwaggerExcludeAttribute>() != null); 
     foreach (var excludedProperty in excludedProperties) 
     { 
      if (schema.properties.ContainsKey(excludedProperty.Name)) 
       schema.properties.Remove(excludedProperty.Name); 
     } 
    } 

    #endregion 
} 

不要忘记注册过滤

c.SchemaFilter<SwaggerExcludeFilter>(); 
+0

看来,这并不只针对输出型号工作?当我在输入模型(GET使用)上应用此代码时,找不到该模型? –

0

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<SwaggerExcludeAttribute>() != null); 
     foreach (PropertyInfo excludedProperty in excludedProperties) 
     { 
      if (schema.Properties.ContainsKey(excludedProperty.Name)) 
      { 
       schema.Properties.Remove(excludedProperty.Name); 
      } 
     } 
    } 
}