例の追加
パラメータ、プロパティ、およびオブジェクトに例を追加することで、Web サービスの OpenAPI Specification をより明確にすることができます。例は、API を何らかの方法で処理するツールやライブラリによって読み取ることができます。たとえば、API モックツールはサンプル値を使用してモックリクエストを生成できます。オブジェクト、個々のプロパティ、および操作パラメータの例を指定できます。例を指定するには、example
または examples
キーを使用します。詳細は以下を参照してください。
Swagger UI ユーザーへの注意: 複数の examples
のサポートは、Swagger UI 3.23.0 および Swagger Editor 3.6.31 以降で利用可能です。
注意: 例の値とデフォルトの値を混同しないでください。例は値がどうあるべきかを示します。デフォルト値は、クライアントが値を提供しない場合にサーバーが使用する値です。
パラメータの例
以下はパラメータ値の例です。
1parameters:2 - in: query3 name: status4 schema:5 type: string6 enum: [approved, pending, closed, new]7 example: approved # Example of a parameter value
パラメータの複数の例
1parameters:2 - in: query3 name: limit4 schema:5 type: integer6 maximum: 507 examples: # Multiple examples8 zero: # Distinct name9 value: 0 # Example value10 summary: A sample limit value # Optional description11 max: # Distinct name12 value: 50 # Example value13 summary: A sample limit value # Optional description
ご覧のとおり、各例にはそれぞれ異なるキー名があります。また、上記のコードでは、オプションのsummary
キーを説明と共に使用しました。注:指定するサンプル値は、パラメータのデータ型と一致する必要があります。
リクエストとレスポンスボディの例
リクエストボディ内のexample
キーワードの例を次に示します
1paths:2 /users:3 post:4 summary: Adds a new user5 requestBody:6 content:7 application/json:8 schema: # Request body contents9 type: object10 properties:11 id:12 type: integer13 name:14 type: string15 example: # Sample object16 id: 1017 name: Jessica Smith18 responses:19 "200":20 description: OK
上記のコードでは、example
がschema
の子であることに注意してください。schema
がcomponents
セクションで定義された何らかのオブジェクトを参照している場合、example
をメディアタイプキーワードの子にする必要があります。
1paths:2 /users:3 post:4 summary: Adds a new user5 requestBody:6 content:7 application/json: # Media type8 schema: # Request body contents9 $ref: "#/components/schemas/User" # Reference to an object10 example: # Child of media type because we use $ref above11 # Properties of a referenced object12 id: 1013 name: Jessica Smith14 responses:15 "200":16 description: OK
これは、$ref
がその横にあるすべての兄弟を上書きするためです。必要に応じて、複数の例を使用できます。
1paths:2 /users:3 post:4 summary: Adds a new user5 requestBody:6 content:7 application/json: # Media type8 schema: # Request body contents9 $ref: "#/components/schemas/User" # Reference to an object10 examples: # Child of media type11 Jessica: # Example 112 value:13 id: 1014 name: Jessica Smith15 Ron: # Example 216 value:17 id: 1118 name: Ron Stewart19 responses:20 "200":21 description: OK
レスポンスボディ内のexample
の例を次に示します。
1responses:2 "200":3 description: A user object.4 content:5 application/json:6 schema:7 $ref: "#/components/schemas/User" # Reference to an object8 example:9 # Properties of the referenced object10 id: 1011 name: Jessica Smith
レスポンスボディの複数の例
1responses:2 "200":3 description: A user object.4 content:5 application/json:6 schema:7 $ref: "#/components/schemas/User" # Reference to an object8 examples:9 Jessica:10 value:11 id: 1012 name: Jessica Smith13 Ron:14 value:15 id: 2016 name: Ron Stewart
注: レスポンスおよびリクエストボディの例は自由形式ですが、ボディスキーマと互換性があることが期待されます。
オブジェクトとプロパティの例
components
セクションで、オブジェクトと個々のプロパティの例を指定することもできます。
- プロパティレベルの例
1components:2 schemas:3 User: # Schema name4 type: object5 properties:6 id:7 type: integer8 format: int649 example: 1 # Property example10 name:11 type: string12 example: New order # Property example
- オブジェクトレベルの例
1components:2 schemas:3 User: # Schema name4 type: object5 properties:6 id:7 type: integer8 name:9 type: string10 example: # Object-level example11 id: 112 name: Jessica Smith
スキーマとプロパティは単一のexample
をサポートしますが、複数のexamples
はサポートしないことに注意してください。
配列の例
個々の配列項目の例を追加できます
1components:2 schemas:3 ArrayOfInt:4 type: array5 items:6 type: integer7 format: int648 example: 1
または、複数の項目を含む配列レベルの例
1components:2 schemas:3 ArrayOfInt:4 type: array5 items:6 type: integer7 format: int648 example: [1, 2, 3]
配列にオブジェクトが含まれる場合、複数項目の例は次のように指定できます
1components:2 schemas:3 ArrayOfUsers:4 type: array5 items:6 type: object7 properties:8 id:9 type: integer10 name:11 type: string12 example:13 - id: 1014 name: Jessica Smith15 - id: 2016 name: Ron Stewart
配列と配列項目は単一のexample
をサポートしますが、複数のexamples
はサポートしないことに注意してください。
XML および HTML データの例
JSONまたはYAML形式で表現できない例の値を記述するには、文字列として指定します
1content:2 application/xml:3 schema:4 $ref: "#/components/schemas/xml"5 examples:6 xml:7 summary: A sample XML response8 value: "<objects><object><id>1</id><name>new</name></object><object><id>2</id></object></objects>"9 text/html:10 schema:11 type: string12 examples:13 html:14 summary: A list containing two items15 value: "<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>"
YAMLでの複数行文字列の記述に関する情報は、このStack Overflowの投稿で見つけることができます。https://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines。
外部の例
何らかの理由でサンプル値を仕様に挿入できない場合(たとえば、YAMLまたはJSONに準拠していない場合)、externalValue
キーワードを使用して例の値のURLを指定できます。URLは、リテラルの例のコンテンツ(オブジェクト、ファイル、画像など)を含むリソースを指す必要があります
1content:2 application/json:3 schema:4 $ref: "#/components/schemas/MyObject"5 examples:6 jsonObject:7 summary: A sample object8 externalValue: "http://example.com/examples/object-example.json"9 application/pdf:10 schema:11 type: string12 format: binary13 examples:14 sampleFile:15 summary: A sample file16 externalValue: "http://example.com/examples/example.pdf"
例の再利用
一般的な例は、仕様のcomponents/examples
セクションで定義し、さまざまなパラメーター記述、リクエストおよびレスポンスボディ記述、オブジェクトおよびプロパティで再利用できます
1 content:2 application/json:3 schema:4 $ref: '#/components/schemas/MyObject'5 examples:6 objectExample:7 $ref: '#/components/examples/objectExample'8 ...9 components:10 examples:11 objectExample:12 value:13 id: 114 name: new object15 summary: A sample object
お探しのものが見つかりませんでしたか? コミュニティに質問する
間違いを見つけましたか? お知らせください