OpenAPI 2.0 の追加プロパティの許可方針の確認

additionalProperties をデータ契約に合わせて選択してください。

説明

OpenAPI 2.0 の additionalProperties は properties にないフィールドの扱いを決めます。真偽値と値のスキーマのどちらも指定できます。

想定される影響

範囲が広すぎると想定外のフィールドを受け入れ、狭すぎるとクライアントが使うフィールドを拒否します。

対処方法

定義済みのフィールドだけを許可する場合は false を指定します。動的なキーの値が同じ型に従う場合はスキーマを指定します。任意の追加値を許可する場合は true にするか省略します。サーバーの検証処理で結果を確認します。

例

変更前のレスポンスは定義済みのフィールドだけを許可します。

変更前

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "tag": {
                  "type": "string"
                }
              },
              "required": [
                "name"
              ],
              "additionalProperties": false
            }
          }
        }
      }
    }
  }
}

変更後

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "tag": {
                  "type": "string"
                }
              },
              "required": [
                "name"
              ],
              "additionalProperties": {
                "$ref": "#/definitions/User"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "User": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "tag": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ]
    }
  }
}

変更後は値が User スキーマに合う追加キーを許可します。この拡張に対応する API で使います。

参考資料