レスポンスオブジェクトの参照先が不正(OpenAPI 3.0)

レスポンスがスキーマなど別の種類のオブジェクトを参照している状態

説明

ステータスコードの $ref はレスポンスオブジェクトを指す必要があります。このオブジェクトには説明があり、ヘッダーや本文の形式も指定できます。

想定される影響

スキーマを直接参照すると、検証に失敗したり、生成した文書に誤ったレスポンス情報が載ったりします。

対処方法

#/components/responses/... または外部ファイルのレスポンスオブジェクトを参照してください。本文スキーマがある場合は、content 内の該当するメディアタイプの schema に指定します。

例

GET / の 200 レスポンスは #/components/responses/Success を参照し、レスポンスの説明と JSON の例を取得します。

変更前

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "components": {
    "responses": {
      "Success": {
        "description": "200 response",
        "content": {
          "application/json": {
            "examples": {
              "foo": {
                "value": {
                  "versions": [
                    {
                      "status": "CURRENT",
                      "updated": "2011-01-21T11:33:21Z",
                      "id": "v2.0",
                      "links": [
                        {
                          "href": "http://127.0.0.1:8774/v2/",
                          "rel": "self"
                        }
                      ]
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "$ref": "#/components/schemas/Success"
          }
        }
      }
    }
  }
}

変更後

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "components": {
    "responses": {
      "Success": {
        "description": "200 response",
        "content": {
          "application/json": {
            "examples": {
              "foo": {
                "value": {
                  "versions": [
                    {
                      "status": "CURRENT",
                      "updated": "2011-01-21T11:33:21Z",
                      "id": "v2.0",
                      "links": [
                        {
                          "href": "http://127.0.0.1:8774/v2/",
                          "rel": "self"
                        }
                      ]
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "$ref": "#/components/responses/Success"
          }
        }
      }
    }
  }
}

参考資料