OpenAPI 2.0 のモデルのプロパティ名の確認

各モデルの意味に合う名前を付け、不要な API 契約の変更を避けてください。

説明

Swagger 2.0 では、別々のオブジェクトスキーマに同じフィールド名を使えます。各オブジェクトで意味が明らかなら名前を維持してください。

想定される影響

公開済みのフィールドを改名すると、既存クライアントでリクエストの送信やレスポンスの処理に失敗する場合があります。

対処方法

意味が曖昧なら説明を補い、共通構造は必要に応じて $ref で再利用します。改名する場合は実際のデータで既存クライアントへの影響を確認してください。

例

操作の本文と再利用パラメーターには、どちらも name、address、age を使えます。参照先の Address 定義は省略しています。

変更前

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "basePath": "/api",
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "name": "limit2",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "address": {
                  "$ref": "#/definitions/Address"
                },
                "age": {
                  "type": "integer",
                  "format": "int32"
                }
              }
            }
          }
        ]
      }
    }
  },
  "parameters": {
    "limitParam": {
      "name": "limit",
      "in": "body",
      "description": "max records to return",
      "required": true,
      "schema": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "address": {
            "$ref": "#/definitions/Address"
          },
          "age": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }
}

変更後

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "basePath": "/api",
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "name": "limit2",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "address": {
                  "$ref": "#/definitions/Address"
                },
                "age": {
                  "type": "integer",
                  "format": "int32"
                }
              }
            }
          }
        ]
      }
    }
  },
  "parameters": {
    "limitParam": {
      "name": "limit",
      "in": "body",
      "description": "max records to return",
      "required": true,
      "schema": {
        "type": "object",
        "properties": {
          "name_2": {
            "type": "string"
          },
          "address_2": {
            "$ref": "#/definitions/Address"
          },
          "age_2": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }
}

_2 が付くのは再利用パラメーター limitParam のフィールドだけです。例の GET 操作は個別に定義した本文スキーマを使います。

参考資料