リクエスト本文オブジェクトの参照先が不正(OpenAPI 3.0)

リクエスト本文がスキーマなど別の種類のオブジェクトを参照している状態

説明

requestBody の $ref は、送信するメディアタイプを定義したリクエスト本文オブジェクトを指す必要があります。

想定される影響

スキーマを直接参照すると、文書から形式が抜け、クライアントが誤った本文を送る場合があります。

対処方法

#/components/requestBodies/... または外部ファイルのリクエスト本文を参照してください。スキーマはそのオブジェクトの content で、該当するメディアタイプの下に指定します。

例

POST / は #/components/requestBodies/List を参照します。List は text/plain の本文に整数配列のスキーマを指定しています。

変更前

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "components": {
    "requestBodies": {
      "List": {
        "description": "id of api version",
        "content": {
          "text/plain": {
            "schema": {
              "type": "array",
              "items": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/": {
      "post": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "requestBody": {
          "$ref": "#/components/schemas/List"
        },
        "responses": {
          "200": {
            "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"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

変更後

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "components": {
    "requestBodies": {
      "List": {
        "description": "id of api version",
        "content": {
          "text/plain": {
            "schema": {
              "type": "array",
              "items": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/": {
      "post": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "requestBody": {
          "$ref": "#/components/requestBodies/List"
        },
        "responses": {
          "200": {
            "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"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

参考資料