未使用のスキーマコンポーネント(OpenAPI 3.0)

実際のデータ契約に使わないモデル定義が残っている状態

説明

components.schemas のモデルが OpenAPI 3.0 の操作に適用されるのは、操作から直接または間接的に使われる場合です。

想定される影響

未使用のモデルが文書や生成コードに残ると、API が実際に受け付けるフィールドや返すフィールドが分かりにくくなります。

対処方法

リクエスト、レスポンス、パラメーター、他のモデル、外部文書からの参照をたどります。どこからも使われていないスキーマだけを削除します。

例

レスポンス本文は MyObject を使用します。例では、参照されていない MyObject2 を削除しています。

変更前

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MyObject"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      },
      "MyObject2": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

変更後

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MyObject"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

参考資料