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

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

説明

OpenAPI 3.0 の components.responses は、再利用可能な応答定義を保持します。存在しない応答を $ref で参照すると、そのステータスコードの説明、ヘッダー、本文構造を文書で解釈できない場合があります。

想定される影響

  • API 利用者がエラー応答や正常応答の処理方法を誤解する可能性があります。
  • 参照エラーにより仕様書の検証やコード生成が失敗する場合があります。

対処方法

応答の $ref が実在する Response オブジェクトを指すか確認してください。ローカル参照を components.responses の定義と一致させ、応答の description と本文スキーマも確認してください。変更後にステータスコードと応答構造が実際の動作に合うか検証してください。

例

以下は 404 応答の参照先の比較です。最初の例の NotRight は存在しません。

変更前

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"
            }
          }
        }
      }
    }
  }
}

変更後は NotFound を参照し、応答の説明と Error スキーマを接続しています。Error のスキーマ直下にある required は、code と message を必須フィールドに指定します。

参考資料