2016-01-25 9 views
7

Swaggerメタデータが自分のリソースでサポートされているコンテンツタイプを含むように、ASP.NET WebAPIアクションに注釈を付けるにはどうすればよいですか?Swashbuckle Swagger - コンテンツタイプに注釈を付ける方法は?

具体的には、私はドキュメントが私の資源の一つは、「オリジナル」application/jsonapplication/xmlを返すだけでなく、新しい形式、application/vnd.blah+jsonまたは+xmlを返すことができることを示したいと思います。

+0

Web Apiの設定中にMediaTypeFormatterを登録すると、Shashbuckle 5がこれを処理する必要があります。 –

+0

ありがとうございます。それは賢く聞こえるが、私はアクション/ルートごとにそれを望む。 –

+1

私はあなたのwebconfigのフォーマッタとして追加する必要があると思います。これはアクション単位ではなく、グローバルです。 あなたができることは、独自のoperationsFilterを作成し、新しいフォーマットを返す操作にのみ適用することです。 – VisualBean

答えて

7

あなたがする必要があるのはこれです。 闊歩仕様: あなたはこれがOperationFilter

擬似コードの着信を行うことができます

"produces": [ 
      "application/json", 
      "text/json" 
      ], 

その操作に応答タイプのリストにあなたの応答タイプを追加する必要があります!

public class CustomResponseType : IOperationFilter 
{   
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    {    
      if (operation.operationId == "myCustomName") 
      { 
       operation.produces.Add("application/vnd.blah+json"); 
      }    
    }  
} 

OperationIdは[SwaggerOperation("myCustomName")]注釈を介して設定することができます。あなたが特定のルートまたは基本的には何かのためにそれを行うことができ 代わりのoperation.operationId == "myCustomName"

次に注意

c.OperationFilter<CustomResponseType>(); 

swaggerConfig.csでoperationsFilterを適用します。 ApiDescriptionは、文脈に関する情報をたくさん提供します。

26

あなたのような属性を設定するには、以下のコードを使用することができますコントローラのAPIメソッドでVisualBeanの答え

@拡張:

[SwaggerResponseContentType(responseType:"application/pdf", Exclusive=true)] 
public HttpResponseMessage GetAuthorityForm(string id) 
{ 
.... 

注:「真=独占」他のすべてのコンテンツの種類を削除しますが、それ以外の場合は、新しい属性を使用すると、Swagger UIドロップダウンに新しいレスポンスコンテンツタイプが追加されます。それはあなたのコントローラまたはAPIをドキュメントだけ変更することはありません。

SwaggerConfig.cs

GlobalConfiguration.Configuration 
      .EnableSwagger(c => 
// Set filter to apply Custom Content Types to operations 
// 
c.OperationFilter<ResponseContentTypeOperationFilter>(); 

SwaggerReponseContentTypeAttribute.cs

/// <summary> 
/// SwaggerResponseContentTypeAttribute 
/// </summary> 
[AttributeUsage(AttributeTargets.Method)] 
public sealed class SwaggerResponseContentTypeAttribute : Attribute 
{ 
    /// <summary> 
    /// SwaggerResponseContentTypeAttribute 
    /// </summary> 
    /// <param name="responseType"></param> 
    public SwaggerResponseContentTypeAttribute(string responseType) 
    { 
     ResponseType = responseType; 
    } 
    /// <summary> 
    /// Response Content Type 
    /// </summary> 
    public string ResponseType { get; private set; } 

    /// <summary> 
    /// Remove all other Response Content Types 
    /// </summary> 
    public bool Exclusive { get; set; } 
} 

ResponseContentTypeOperationFilter.cs

public class ResponseContentTypeOperationFilter : IOperationFilter 
{ 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    { 
     var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>().FirstOrDefault(); 

     if (requestAttributes != null) 
     { 
      if (requestAttributes.Exclusive) 
       operation.produces.Clear(); 

      operation.produces.Add(requestAttributes.ResponseType); 
     } 
    } 
} 
5

@ OzBobの答えは、メソッドに単一の属性だけを追加したいと仮定しています。あなたは、同じ方法で複数のコンテンツタイプを追加し、文書化したい場合は、以下を使用することができます。

SwaggerReponseContentTypeAttribute.cs

/// <summary> 
/// SwaggerResponseContentTypeAttribute 
/// </summary> 
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] 
public class SwaggerResponseContentTypeAttribute : Attribute 
{ 
    /// <summary> 
    /// SwaggerResponseContentTypeAttribute 
    /// </summary> 
    /// <param name="responseType"></param> 
    public SwaggerResponseContentTypeAttribute(string responseType) 
    { 
     ResponseType = responseType; 
    } 
    /// <summary> 
    /// Response Content Type 
    /// </summary> 
    public string ResponseType { get; private set; } 

    /// <summary> 
    /// Remove all other Response Content Types 
    /// </summary> 
    public bool Exclusive { get; set; } 
} 

ResponseContentTypeOperationFilter。あなたは、同じ方法で複数の属性を持っていて、既存のコンテンツタイプを交換したいとき、あなただけの最初の属性にExclusive = trueを設定する必要があることをCS

public class ResponseContentTypeOperationFilter : IOperationFilter 
{ 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    { 
     var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>(); 

     foreach (var requestAttribute in requestAttributes) 
     { 
      if (requestAttribute.Exclusive) 
      { 
       operation.produces.Clear(); 
      } 
      operation.produces.Add(requestAttribute.ResponseType); 
     } 
    } 
} 

注意。そうしないと、すべての属性がドキュメントに反映されません。

+1

最初の属性としてExclusive = trueを設定することは、開発者にとって明らかではありません。私は属性の順序が出力に影響してはならないと思います。フィルタの属性リストをソートすると、制限が解除されます。requestAttributes.OrderByDescending(a => a.Exclusive) –

関連する問題