2016-06-14 3 views
7

WebApi 2プロジェクトでSwashbuckleを使用してスワッガードキュメントを構築しています。 Swaggerファイルを生成するために200 OKレスポンスを自動的に追加する

は、私は方法の次の定義を持っている:

[HttpPost] 
[ResponseType(typeof(Reservation))] 
[Route("reservations")] 
[SwaggerResponse(HttpStatusCode.Created, Type = typeof(Reservation))] 
[SwaggerResponse(HttpStatusCode.BadRequest) ] 
[SwaggerResponse(HttpStatusCode.Conflict)] 
[SwaggerResponse(HttpStatusCode.NotFound)] 
[SwaggerResponse(HttpStatusCode.InternalServerError)]   
public async Task<HttpResponseMessage> ReserveTickets([FromBody] ReserveTicketsRequest reserveTicketRequest) 
{ 
    // ... 
    return Request.CreateResponse(HttpStatusCode.Created, response); 
} 

それはどこにも指定されていないが、生成された闊歩ファイルは、同様にHTTP 200 OKが含まれていますが。

/reservations: 
    post: 
    tags: 
     - "Booking" 
    operationId: "Booking_ReserveTickets" 
    consumes: 
     - "application/json" 
     - "text/json" 
    produces: 
     - "application/json" 
     - "text/json" 
    parameters: 
     - 
     name: "reserveTicketRequest" 
     in: "body" 
     required: true 
     schema: 
      $ref: "#/definitions/ReserveTicketsRequest" 
    responses: 
     200: 
     description: "OK" 
     schema: 
      $ref: "#/definitions/Reservation" 
     201: 
     description: "Created" 
     schema: 
      $ref: "#/definitions/Reservation" 
     400: 
     description: "BadRequest" 
     404: 
     description: "NotFound" 
     409: 
     description: "Conflict" 
     500: 
     description: "InternalServerError" 
    deprecated: false 

200 OKを取り除く方法はありますか?それは有効な応答ではないので混乱します。

ありがとうございます。

答えて

9

SwaggerResponseRemoveDefaults属性を持つメソッドをデコレートすると、デフォルトの応答(200 OK)を削除できます。

+0

ありがとうございます!これはトリックでした! –

+0

コントローラの各メソッドを飾るのではなく、これをグローバルに追加する方法はありますか? – michaelmsm89

+0

この属性はコントローラにも適用できます。この動作をグローバルに追加するには、独自の 'IDocumentFilter'を作成する必要があります。 – venerik

関連する問題