はじめに
実務で Swagger UI
を使用したときにレスポンスの形式で詰まった箇所がありましたので同じく詰まった方の参考になれば幸いです。
Swagger
については、他にも多くの記事があるので今回は触れません。
参考にさせていただいた記事は参考文献として載せさせていただきました。
レスポンス形式
基本的に Swagger Editor を参考にしております。
下のように {}
の中で []
を入れ子にしております。
想定しているレスポンス
swagger
{
"pets": [
{
"pet_id": 1,
"name": "ポチ"
}
]
}
swaggerの記述例
swagger
paths:
/pet:
get:
tags:
- "pet"
summary: "使用例"
description: ""
consumes:
- "application/json"
produces:
- "application/json"
responses:
'200':
description: "オブジェクトの中で配列の使用"
schema:
$ref: '#/components/schemas/pet'
components:
schemas:
pet:
type: object
properties:
pets:
type: array
items:
type: object
properties:
pet_id:
type: integer
format: int64
description: ペットの識別ID
example: 1
name:
type: string
description: petの名前
example: ポチ