OpenAPI 3.0 の応答参照先が存在しない

応答の参照先を実際の応答定義に接続してください。

説明

ステータスコードでローカルの共通レスポンスを使う場合は、$ref の名前を components.responses の定義名に合わせます。外部ファイルのレスポンスオブジェクトも参照できます。

想定される影響

参照先がないと、クライアントの生成に失敗したり、利用者がそのレスポンスの扱い方を判断できなかったりします。

対処方法

参照を修正して仕様を検証してください。ステータスコード、説明、ヘッダー、本文の構造がサーバーの返すレスポンスと一致することも確認します。

例

変更前

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "404": {
            "$ref": "#/components/responses/NotRight"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          },
          "message": {
            "type": "string"
          }
        },
        "required": [
          "code",
          "message"
        ]
      }
    },
    "responses": {
      "NotFound": {
        "description": "Resource not found",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    }
  }
}

変更後

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          },
          "message": {
            "type": "string"
          }
        },
        "required": [
          "code",
          "message"
        ]
      }
    },
    "responses": {
      "NotFound": {
        "description": "Resource not found",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    }
  }
}

GET / の 404 レスポンスは、未定義の NotRight を NotFound に変更しています。JSON 本文の Error スキーマは、文字列の code と message を必須としています。

参考資料