レスポンスの記述
API仕様は、すべてのAPI操作に対してresponses
を指定する必要があります。各操作には、少なくとも1つのレスポンス(通常は成功レスポンス)が定義されている必要があります。レスポンスは、HTTPステータスコードと、レスポンスボディやヘッダーで返されるデータによって定義されます。以下に最小限の例を示します。
1paths:2 /ping:3 get:4 produces:5 - application/json6 responses:7 200:8 description: OK
レスポンスメディアタイプ
APIはさまざまなメディアタイプで応答できます。JSONはデータ交換に最も一般的な形式ですが、唯一の形式ではありません。レスポンスのメディアタイプを指定するには、ルートレベルまたは操作レベルでproduces
キーワードを使用します。グローバルリストは操作レベルでオーバーライドできます。
1produces:2 - application/json3
4paths:5 # This operation returns JSON - as defined globally above6 /users:7 get:8 summary: Gets a list of users.9 responses:10 200:11 description: OK12 # Here, we override the "produces" array to specify other media types13 /logo:14 get:15 summary: Gets the logo image.16 produces:17 - image/png18 - image/gif19 - image/jpeg20 responses:21 200:22 description: OK
詳細情報: MIMEタイプ。
HTTP ステータスコード
responses
の下では、各レスポンス定義は200や404などのステータスコードから始まります。操作は通常、1つの成功ステータスコードと1つ以上のエラー状態を返します。各レスポンスステータスにはdescription
が必要です。たとえば、エラーレスポンスの条件を記述できます。GitHub Flavored Markdownは、リッチテキスト表現に使用できます。
1responses:2 200:3 description: OK4 400:5 description: Bad request. User ID must be an integer and bigger than 0.6 401:7 description: Authorization information is missing or invalid.8 404:9 description: A user with the specified ID was not found.
API仕様は、事前に知ることができない可能性があるため、すべての可能なHTTPレスポンスコードをカバーする必要があるわけではないことに注意してください。ただし、成功レスポンスと既知のエラーはカバーすることが期待されます。「既知のエラー」とは、たとえば、IDでリソースを返す操作に対する404 Not Foundレスポンス、または無効な操作パラメータの場合の400 Bad Requestレスポンスを意味します。
レスポンスボディ
schema
キーワードは、レスポンスボディを記述するために使用されます。スキーマは次を定義できます。
object
またはarray
– 通常、JSONおよびXML APIで使用されます。- 数値や文字列などのプリミティブ – プレーンテキストレスポンスに使用されます。
file
(下記参照)。
スキーマは操作内でインラインで定義できます。
1responses:2 200:3 description: A User object4 schema:5 type: object6 properties:7 id:8 type: integer9 description: The user ID.10 username:11 type: string12 description: The user name.
または、ルートレベルで定義し、$ref
を介して参照することもできます。これは、複数のレスポンスが同じスキーマを使用する場合に便利です。
1 responses:2 200:3 description: A User object4 schema:5 $ref: '#/definitions/User'6definitions:7 User:8 type: object9 properties:10 id:11 type: integer12 description: The user ID.13 username:14 type: string15 description: The user name.
ファイルを返すレスポンス
API操作は、画像やPDFなどのファイルを返すことができます。この場合、schema
をtype: file
で定義し、produces
セクションで適切なMIMEタイプを指定します。
1paths:2 /report:3 get:4 summary: Returns the report in the PDF format.5 produces:6 - application/pdf7 responses:8 200:9 description: A PDF file.10 schema:11 type: file
空のレスポンスボディ
204 No Contentなどの一部のレスポンスにはボディがありません。レスポンスボディが空であることを示すには、レスポンスのschema
を指定しないでください。Swaggerは、スキーマがないことをボディのないレスポンスとして扱います。
1responses:2 204:3 description: The resource was deleted successfully.
レスポンスヘッダー
APIからのレスポンスには、API呼び出しの結果に関する追加情報を提供するためのカスタムヘッダーを含めることができます。たとえば、レート制限のあるAPIは、次のようにレスポンスヘッダーを介してレート制限ステータスを提供できます。
1HTTP 1/1 200 OK2X-RateLimit-Limit: 1003X-RateLimit-Remaining: 994X-RateLimit-Reset: 2016-10-12T11:00:00Z5
6{ ... }
各レスポンスにカスタムheaders
を次のように定義できます。
1paths:2 /ping:3 get:4 summary: Checks if the server is alive.5 responses:6 200:7 description: OK8 headers:9 X-RateLimit-Limit:10 type: integer11 description: Request limit per hour.12 X-RateLimit-Remaining:13 type: integer14 description: The number of requests left for the time window.15 X-RateLimit-Reset:16 type: string17 format: date-time18 description: The UTC date/time at which the current rate limit window resets.
現在、Swaggerでは、異なるレスポンスコードまたは異なるAPI操作に対して共通のレスポンスヘッダーを定義する方法がないことに注意してください。ヘッダーは各レスポンスごとに個別に定義する必要があります。
デフォルトレスポンス
場合によっては、操作は異なるHTTPステータスコードで複数のエラーを返すことがありますが、それらすべてが同じレスポンス構造を持っています。
1responses:2 200:3 description: Success4 schema:5 $ref: "#/definitions/User"6 400:7 description: Bad request8 schema:9 $ref: "#/definitions/Error" # <-----10 404:11 description: Not found12 schema:13 $ref: "#/definitions/Error" # <-----
default
レスポンスを使用して、これらのエラーを個別にではなくまとめて記述できます。「デフォルト」とは、この操作で個別にカバーされていないすべてのHTTPコードにこのレスポンスが使用されることを意味します。
1responses:2 200:3 description: Success4 schema:5 $ref: "#/definitions/User"6 # Definition of all error statuses7 default:8 description: Unexpected error9 schema:10 $ref: "#/definitions/Error"
レスポンスの再利用
複数の操作が同じレスポンス(ステータスコードとデータ)を返す場合、それをグローバルのresponses
セクションで定義し、操作レベルで$ref
を介してその定義を参照できます。これは、同じステータスコードとレスポンスボディを持つエラーレスポンスに役立ちます。
1paths:2 /users:3 get:4 summary: Gets a list of users.5 response:6 200:7 description: OK8 schema:9 $ref: "#/definitions/ArrayOfUsers"10 401:11 $ref: "#/responses/Unauthorized" # <-----12 /users/{id}:13 get:14 summary: Gets a user by ID.15 response:16 200:17 description: OK18 schema:19 $ref: "#/definitions/User"20 401:21 $ref: "#/responses/Unauthorized" # <-----22 404:23 $ref: "#/responses/NotFound" # <-----24# Descriptions of common responses25responses:26 NotFound:27 description: The specified resource was not found28 schema:29 $ref: "#/definitions/Error"30 Unauthorized:31 description: Unauthorized32 schema:33 $ref: "#/definitions/Error"34definitions:35 # Schema for error response body36 Error:37 type: object38 properties:39 code:40 type: string41 message:42 type: string43 required:44 - code45 - message
ルートレベルで定義されたレスポンスは、すべての操作に自動的に適用されるわけではないことに注意してください。これらは、複数の操作で参照および再利用できる定義にすぎません。
FAQ
リクエストパラメータに基づいて異なるレスポンスを持つことはできますか?たとえば、
1GET /something -> {200, schema_1}2GET /something?foo=bar -> {200, schema_2}
いいえ、これはサポートされていません。
参照
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#responsesDefinitionsObject
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#responsesObject
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#responseObject
お探しのものが見つかりませんでしたか? コミュニティに質問する
間違いを見つけましたか? お知らせください