パラメータの記述
OpenAPI 3.0では、パラメータは操作またはパスのparameters
セクションで定義されます。パラメータを記述するには、そのname
、場所(in
)、データ型(schema
またはcontent
のいずれかで定義)、およびdescription
やrequired
などの他の属性を指定します。以下に例を示します。
1paths:2 /users/{userId}:3 get:4 summary: Get a user by ID5 parameters:6 - in: path7 name: userId8 schema:9 type: integer10 required: true11 description: Numeric ID of the user to get
parameters
は配列であるため、YAMLでは各パラメータ定義の前にダッシュ(-
)を付けてリストする必要があることに注意してください。
パラメータタイプ
OpenAPI 3.0では、パラメータの場所に基づいて、以下のパラメータタイプを区別しています。場所は、パラメータのin
キーによって決定されます。例えば、in: query
またはin: path
です。
- パスパラメータ (例:
/users/{id}
) - クエリパラメータ (例:
/users?role=admin
) - ヘッダーパラメータ (例:
X-MyHeader: Value
) - クッキーパラメータ (例:
Cookie: debug=0; csrftoken=BUSe35dohU3O1MZvDCU
のようにCookie
ヘッダーで渡される)
パスパラメータ
パスパラメータは、URLパスの可変部分です。これらは通常、IDで識別されるユーザーなど、コレクション内の特定の¹リソースを指すために使用されます。URLには複数のパスパラメータを含めることができ、それぞれ中括弧{ }
で示されます。
1GET /users/{id}2GET /cars/{carId}/drivers/{driverId}3GET /report.{format}
クライアントがAPIコールを行う際、各パスパラメータは実際の値に置き換えられる必要があります。OpenAPIでは、パスパラメータはin: path
を使用して定義されます。パラメータ名は、パスで指定されたものと同じである必要があります。また、パスパラメータは常に必須であるため、required: true
を追加することを忘れないでください。例えば、/users/{id}
エンドポイントは次のように記述されます。
1paths:2 /users/{id}:3 get:4 parameters:5 - in: path6 name: id # Note the name is the same as in the path7 required: true8 schema:9 type: integer10 minimum: 111 description: The user ID
配列とオブジェクトを含むパスパラメータは、さまざまな方法でシリアル化できます
- パススタイル展開 (matrix) – セミコロン区切り、例:
/map/point;x=50;y=20
- ラベル展開 – ドット区切り、例:
/color.R=100.G=200.B=150
- シンプルスタイル – カンマ区切り、例:
/users/12,34,56
シリアル化方法は、style
およびexplode
キーワードで指定されます。詳細については、パラメータのシリアル化を参照してください。
クエリパラメータ
クエリパラメータは、最も一般的なタイプのパラメータです。これらは、リクエストURLの末尾に疑問符(?
)の後に現れ、異なるname=value
ペアがアンパサンド(&
)で区切られます。クエリパラメータは必須とオプションの両方がありえます。
1GET /pets/findByStatus?status=available2GET /notes?offset=100&limit=50
クエリパラメータを示すにはin: query
を使用します
1parameters:2 - in: query3 name: offset4 schema:5 type: integer6 description: The number of items to skip before starting to collect the result set7 - in: query8 name: limit9 schema:10 type: integer11 description: The numbers of items to return
注:クエリパラメータとして渡されるAPIキーを記述するには、代わりにsecuritySchemes
とsecurity
を使用してください。APIキーを参照してください。
クエリパラメータは、プリミティブ値、配列、およびオブジェクトにすることができます。OpenAPI 3.0は、クエリ文字列内のオブジェクトと配列をシリアル化するためのいくつかの方法を提供します。
配列は次のようにシリアル化できます。
form
–explode
キーワードによって、/products?color=blue,green,red
または/products?color=blue&color=green
spaceDelimited
(OpenAPI 2.0のcollectionFormat: ssv
と同じ) –/products?color=blue%20green%20red
pipeDelimited
(OpenAPI 2.0のcollectionFormat: pipes
と同じ) –/products?color=blue|green|red
オブジェクトは以下のようにシリアル化できます。
form
–explode
キーワードによって、/points?color=R,100,G,200,B,150
または/points?R=100&G=200&B=150
deepObject
–/points?color[R]=100&color[G]=200&color[B]=150
シリアル化方法は、style
およびexplode
キーワードで指定されます。詳細については、パラメータのシリアル化を参照してください。
クエリパラメータ内の予約文字
RFC 3986は、URIコンポーネントの区切り文字として使用される予約文字:/?#[]@!$&'()*+,;=
のセットを定義しています。これらの文字をクエリパラメータ値で文字通り使用する必要がある場合、通常はパーセントエンコードされます。たとえば、/
は%2F
(または%2f
)としてエンコードされるため、パラメータ値quotes/h2g2.txt
は次のように送信されます。
1GET /file?path=quotes%2Fh2g2.txt
パーセントエンコードされないクエリパラメータが必要な場合は、パラメータ定義にallowReserved: true
を追加します。
1parameters:2 - in: query3 name: path4 required: true5 schema:6 type: string7 allowReserved: true # <-----
この場合、パラメータ値は次のように送信されます。
1GET /file?path=quotes/h2g2.txt
ヘッダーパラメータ
APIコールでは、HTTPリクエストとともにカスタムヘッダーを送信する必要がある場合があります。OpenAPIを使用すると、カスタムリクエストヘッダーをin: header
パラメータとして定義できます。たとえば、GET /ping
へのコールにX-Request-ID
ヘッダーが必要な場合を想定します。
1 GET /ping HTTP/1.12 Host: example.com3 X-Request-ID: 77e1c83b-7bb0-437b-bc50-a7a58e5660ac
OpenAPI 3.0 を使用して、この操作を次のように定義します。
1paths:2 /ping:3 get:4 summary: Checks if the server is alive5 parameters:6 - in: header7 name: X-Request-ID8 schema:9 type: string10 format: uuid11 required: true
同様に、カスタムレスポンスヘッダーを定義できます。ヘッダーパラメータはプリミティブ、配列、オブジェクトにすることができます。配列とオブジェクトはsimple
スタイルを使用してシリアル化されます。詳細については、パラメータシリアル化を参照してください。
注:Accept
、Content-Type
、Authorization
という名前のヘッダーパラメータは許可されません。これらのヘッダーを記述するには、対応するOpenAPIキーワードを使用してください。
ヘッダー | OpenAPIキーワード | 詳細については… |
---|---|---|
Content-Type |
リクエストコンテンツタイプ: requestBody.content.<media-type> レスポンスコンテンツタイプ: responses.<code>.content.<media-type> |
リクエストボディの記述, レスポンスの記述, メディアタイプ |
Accept |
responses.<code>.content.<media-type> |
レスポンスの記述, メディアタイプ |
認証 |
securitySchemes , security |
認証 |
クッキーパラメータ
操作は、Cookie: name=value
のようにCookie
ヘッダーでもパラメータを渡すことができます。複数のクッキーパラメータは、同じヘッダーでセミコロンとスペースで区切られて送信されます。
1GET /api/users2Host: example.com3Cookie: debug=0; csrftoken=BUSe35dohU3O1MZvDCUOJ
クッキーパラメータを定義するにはin: cookie
を使用します
1parameters:2 - in: cookie3 name: debug4 schema:5 type: integer6 enum: [0, 1]7 default: 08 - in: cookie9 name: csrftoken10 schema:11 type: string
クッキーパラメータはプリミティブ値、配列、オブジェクトにすることができます。配列とオブジェクトはform
スタイルを使用してシリアル化されます。詳細については、パラメータのシリアル化を参照してください。
注:クッキー認証を定義するには、代わりにAPIキーを使用してください。
必須およびオプションのパラメータ
デフォルトでは、OpenAPI はすべてのリクエストパラメータをオプションとして扱います。パラメータを必須としてマークするには、required: true
を追加できます。パスパラメータは常に必須であるため、required: true
を持つ必要があることに注意してください。
1parameters:2 - in: path3 name: userId4 schema:5 type: integer6 required: true # <----------7 description: Numeric ID of the user to get.
スキーマ vs. コンテンツ
パラメータの内容を記述するには、schema
またはcontent
キーワードを使用できます。これらは相互に排他的であり、異なるシナリオで使用されます。ほとんどの場合、schema
を使用します。これは、プリミティブ値、および文字列にシリアル化された単純な配列とオブジェクトを記述できます。配列およびオブジェクトパラメータのシリアル化方法は、そのパラメータで使用されるstyle
およびexplode
キーワードによって定義されます。
1parameters:2 - in: query3 name: color4 schema:5 type: array6 items:7 type: string8
9 # Serialize as color=blue,black,brown (default)10 style: form11 explode: false
content
は、style
やexplode
でカバーされない複雑なシリアル化シナリオで使用されます。たとえば、次のようにクエリ文字列にJSON文字列を送信する必要がある場合です。
1filter={"type":"t-shirt","color":"blue"}
この場合、以下に示すように、パラメータschema
をcontent/<media-type>
でラップする必要があります。schema
はパラメータのデータ構造を定義し、メディアタイプ(この例ではapplication/json
)はシリアル化形式を記述する外部仕様への参照として機能します。
1parameters:2 - in: query3 name: filter4
5 # Wrap 'schema' into 'content.<media-type>'6 content:7 application/json: # <---- media type indicates how to serialize / deserialize the parameter content8 schema:9 type: object10 properties:11 type:12 type: string13 color:14 type: string
Swagger UI および Swagger Editor のユーザーへ: content
を持つパラメータは、Swagger UI 3.23.7 以降および Swagger Editor 3.6.34 以降でサポートされています。
デフォルトのパラメータ値
オプションのパラメータのデフォルト値を指定するには、パラメータスキーマでdefault
キーワードを使用します。デフォルト値とは、クライアントがリクエストでパラメータ値を指定しなかった場合にサーバーが使用する値です。値の型は、パラメータのデータ型と同じでなければなりません。典型的な例は、offset
やlimit
などのページングパラメータです。
1GET /users2GET /users?offset=30&limit=10
offset
がデフォルトで0、limit
がデフォルトで20、範囲が0から100であると仮定すると、これらのパラメータは次のように定義されます。
1parameters:2 - in: query3 name: offset4 schema:5 type: integer6 minimum: 07 default: 08 required: false9 description: The number of items to skip before starting to collect the result set.10 - in: query11 name: limit12 schema:13 type: integer14 minimum: 115 maximum: 10016 default: 2017 required: false18 description: The number of items to return.
よくある間違い
default
キーワードを使用する際に、よくある間違いが2つあります。
- パスパラメータなどの
required
パラメータまたはプロパティと一緒にdefault
を使用すること。これは意味がありません。値が必須である場合、クライアントは常にそれを送信する必要があり、デフォルト値は使用されません。 - サンプル値を指定するために
default
を使用すること。これはdefault
の使用目的ではありません。一部のSwaggerツールで予期しない動作につながる可能性があります。この目的には、代わりにexample
またはexamples
キーワードを使用してください。例の追加を参照してください。
Enumパラメータ
パラメータのschema
にenum
を追加することで、パラメータを固定された値のセットに制限することができます。enum値は、パラメータのデータ型と同じタイプでなければなりません。
1parameters:2 - in: query3 name: status4 schema:5 type: string6 enum:7 - available8 - pending9 - sold
詳細情報: 列挙型の定義。
定数パラメータ
1つの値のみを持つ必須パラメータとして定数パラメータを定義することができます
1parameters:2 - in: query3 name: rel_date4 required: true5 schema:6 type: string7 enum:8 - now
enum
プロパティは可能な値を指定します。この例では、1つの値のみが使用でき、これがSwagger UIでユーザーが選択できる唯一の値となります。
注:定数パラメータは、デフォルトパラメータ値とは異なります。定数パラメータはクライアントによって常に送信されますが、デフォルト値は、クライアントによってパラメータが送信されなかった場合にサーバーが使用するものです。
空値およびヌル許容パラメータ
クエリ文字列パラメータは、次のように名前だけで値がない場合があります。
1GET /foo?metadata
このようなパラメータを記述するには、allowEmptyValue
を使用します。
1parameters:2 - in: query3 name: metadata4 schema:5 type: boolean6 allowEmptyValue: true # <-----
OpenAPI 3.0では、スキーマでnullable
もサポートされており、操作パラメータがnull
値を持つことができます。たとえば、次のスキーマはC#のint?
とJavaのjava.lang.Integer
に対応します。
1schema:2 type: integer3 format: int324 nullable: true
注:nullable
はオプションのパラメータや空値のパラメータとは異なります。nullable
はパラメータ値がnull
になりうることを意味します。特定の実装では、存在しないパラメータや空値のパラメータをnull
にマッピングすることを選択するかもしれませんが、厳密に言えばこれらは同じものではありません。
パラメータ例
パラメータにexample
または複数のexamples
を指定できます。例の値はパラメータスキーマと一致する必要があります。単一の例:
1parameters:2 - in: query3 name: limit4 schema:5 type: integer6 minimum: 17 example: 20
複数の名前付き例
1parameters:2 - in: query3 name: ids4 description: One or more IDs5 required: true6 schema:7 type: array8 items:9 type: integer10 style: form11 explode: false12 examples:13 oneId:14 summary: Example of a single ID15 value: [5] # ?ids=516 multipleIds:17 summary: Example of multiple IDs18 value: [1, 5, 7] # ?ids=1,5,7
詳細については、例の追加を参照してください。
非推奨パラメータ
deprecated: true
を使用して、パラメータを非推奨としてマークします。
1- in: query2 name: format3 required: true4 schema:5 type: string6 enum: [json, xml, yaml]7 deprecated: true8 description: Deprecated, use the appropriate `Accept` header instead.
共通パラメータ
パスのすべてのメソッドに対する共通パラメータ
パスのすべての操作で共有されるパラメータは、操作レベルではなくパスレベルで定義できます。パスレベルのパラメータは、そのパスのすべての操作に継承されます。典型的なユースケースは、パスパラメータを介してアクセスされるリソースを操作するGET/PUT/PATCH/DELETE操作です。
1paths:2 /user/{id}:3 parameters:4 - in: path5 name: id6 schema:7 type: integer8 required: true9 description: The user ID10 get:11 summary: Gets a user by ID12 ...13 patch:14 summary: Updates an existing user with the specified ID15 ...16 delete:17 summary: Deletes the user with the specified ID18 ...
操作レベルで定義された追加のパラメータは、パスレベルのパラメータと一緒に使用されます
1paths:2 /users/{id}:3 parameters:4 - in: path5 name: id6 schema:7 type: integer8 required: true9 description: The user ID.10
11 # GET/users/{id}?metadata=true12 get:13 summary: Gets a user by ID14 # Note we only define the query parameter, because the {id} is defined at the path level.15 parameters:16 - in: query17 name: metadata18 schema:19 type: boolean20 required: false21 description: If true, the endpoint returns only the user metadata.22 responses:23 "200":24 description: OK
特定のパスレベルのパラメータは操作レベルで上書きできますが、削除することはできません。
1paths:2 /users/{id}:3 parameters:4 - in: path5 name: id6 schema:7 type: integer8 required: true9 description: The user ID.10
11 # DELETE /users/{id} - uses a single ID.12 # Reuses the {id} parameter definition from the path level.13 delete:14 summary: Deletes the user with the specified ID.15 responses:16 "204":17 description: User was deleted.18
19 # GET /users/id1,id2,id3 - uses one or more user IDs.20 # Overrides the path-level {id} parameter.21 get:22 summary: Gets one or more users by ID.23 parameters:24 - in: path25 name: id26 required: true27 description: A comma-separated list of user IDs.28 schema:29 type: array30 items:31 type: integer32 minItems: 133 explode: false34 style: simple35 responses:36 "200":37 description: OK
様々なパスの共通パラメータ
異なるAPIパスには、ページネーションパラメータなどの共通パラメータが含まれる場合があります。共通パラメータはグローバルcomponents
セクションのparameters以下に定義し、$ref
を介して他の場所から参照できます。
1components:2 parameters:3 offsetParam: # <-- Arbitrary name for the definition that will be used to refer to it.4 # Not necessarily the same as the parameter name.5 in: query6 name: offset7 required: false8 schema:9 type: integer10 minimum: 011 description: The number of items to skip before starting to collect the result set.12 limitParam:13 in: query14 name: limit15 required: false16 schema:17 type: integer18 minimum: 119 maximum: 5020 default: 2021 description: The numbers of items to return.22
23paths:24 /users:25 get:26 summary: Gets a list of users.27 parameters:28 - $ref: "#/components/parameters/offsetParam"29 - $ref: "#/components/parameters/limitParam"30 responses:31 "200":32 description: OK33 /teams:34 get:35 summary: Gets a list of teams.36 parameters:37 - $ref: "#/components/parameters/offsetParam"38 - $ref: "#/components/parameters/limitParam"39 responses:40 "200":41 description: OK
components
で定義されたパラメータは、すべての操作に適用されるパラメータではなく、簡単に再利用できるグローバル定義であることに注意してください。
パラメータ依存関係
OpenAPI 3.0 はパラメータの依存関係や排他的パラメータをサポートしていません。https://github.com/OAI/OpenAPI-Specification/issues/256 で未解決の機能リクエストがあります。できることとしては、パラメータの説明に制限を記述し、400 Bad Request レスポンスでロジックを定義することです。たとえば、相対日付範囲 (rdate
) または正確な範囲 (start_date
+ end_date
) のいずれかを受け入れる /report
エンドポイントを考えてみましょう。
1GET /report?rdate=Today2GET /report?start_date=2016-11-15&end_date=2016-11-20
このエンドポイントは次のように記述できます。
1paths:2 /report:3 get:4 parameters:5 - name: rdate6 in: query7 schema:8 type: string9 description: >10 A relative date range for the report, such as `Today` or `LastWeek`.11 For an exact range, use `start_date` and `end_date` instead.12 - name: start_date13 in: query14 schema:15 type: string16 format: date17 description: >18 The start date for the report. Must be used together with `end_date`.19 This parameter is incompatible with `rdate`.20 - name: end_date21 in: query22 schema:23 type: string24 format: date25 description: >26 The end date for the report. Must be used together with `start_date`.27 This parameter is incompatible with `rdate`.28 responses:29 "400":30 description: Either `rdate` or `start_date`+`end_date` are required.
参照
お探しのものが見つかりませんでしたか? コミュニティに質問する
間違いを見つけましたか? お知らせください