1
0

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 1 year has passed since last update.

OpenAPIでレスポンスボディに配列を使用する

Last updated at Posted at 2022-06-22

はじめに

OpenAPIに関しての記事が少ない気がする、もしくは僕の検索の仕方が下手なのかわかりませんが、とにかくここにまとめておきます。
間違っていたりしましたらぜひご指摘いただけると嬉しいです。

ちなみに、openAPIのバージョンとしては、3.0.3を使用。

OpenAPIで配列を使用する

ここでは、レスポンスの部分に関しては別ファイルに分割している。postでのレスポンスに関しては、postResponse.yamlというファイルに分割し、そこにレスポンスの内容を記述している。以下が例です。

postResponse.yaml
description: OK
content:
  application/json:
    schema:
      oneOf:
        - type: object
          properties: {}
        - type: object
          properties:
            goriras:
              type: array ## ここtypeをarrayとすること
              items:
                type: object
                description: ゴリラ一覧
                properties:
                  age:
                    type: integer
                    description: 年齢
                    required: true
                  name:
                    type: string
                    description: 名前
                    required: true
    examples:
      ゴリラあり:
        value:
          {
            goriras:
              [
                { age: 2, name: takeshi },
                { age: 23, name: hage },
              ], ## ここのカンマを忘れるとexamplesとしてSwagger Previewで正常に表示されない
          }
      ゴリラなし:
        value: {}

Swagger Previewの表示はどうなるか

ゴリラあり

スクリーンショット 2022-06-23 7.28.36.png
こんな感じで、ゴリラありの場合は配列でレスポンスの値があるのが表現できる。

ゴリラなし

スクリーンショット 2022-06-23 7.28.47.png
ゴリラなしの場合はこのように何もレスポンスがないことを表現することができる。

おわり

書き方だけで詰まる人はたくさんいると思うからここに記しておきます。
詳しいことは自分でググってください。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?