2016-12-06 9 views
3

私はswashbuckleを使用して、webapi2プロジェクトのスワッガードキュメンテーション\ UIを生成しています。私たちのモデルはいくつかのレガシーインターフェースと共有されているので、モデルで無視したいプロパティがいくつかあります。従来のインターフェースもJSONにシリアライズする必要があるため、JsonIgnore属性を使用することはできません。スワッシュバックル設定だけで、プロパティをグローバルに無視したくないためです。モデルのプロパティを無視するようにスワッシュバックルを設定するには

私はここに文書化され、これを行う方法が見つかりました:

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>()); 

をしかし、私は、現在のバージョンでIModelFilterを使用するようにSwashbuckleを設定する方法がわからないんですか?私はSwashbuckle 5.5.3を使用しています。 httpConfiguration.EnableSwaggerを呼び出すときに、次のように私はこのSchemaFilterを使用するようにSwaggerDocsConfigを設定し、その後

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); 
    } 
} 

+0

することができますに実際にJsonIgnoを使用するスワッガーにプロパティを表示しないようにしてください。 –

+0

質問で述べたように、私はモデルを使用する必要があるレガシーコードを持っているのでJsonIgnoreを使いたくありません。レガシーコード... – mutex

答えて

4

まあ、突きのビットで、私はこの使用してISchemaFilterを行う方法を発見した

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

あなたは内部または保護されたとして、フィールド/プロパティをマークするとまたは非公開であれば、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); 
      } 
     } 
    } 
} 
関連する問題