1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swagger】オブジェクトの中に配列を書く方法

Posted at

はじめに

実務で 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: ポチ

参考文献

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?