1つの操作でbodyとformDataを併用(OpenAPI 2.0)

同じAPI操作で本文とフォームのパラメーターを併用している状態

説明

OpenAPI 2.0 の body と formData はどちらもリクエスト本文を表すため、一つの操作では片方だけを使います。

想定される影響

両方を指定すると検証に失敗したり、クライアントがサーバーの読み取れない本文を送ったりします。

対処方法

フォームなら各フィールドに in: formData と type を指定し、サーバーが受け付ける形式を consumes に記述します。構造化された本文なら、schema を持つ in: body パラメーターを一つ使います。

例

どちらの例も application/x-www-form-urlencoded を指定しています。修正例では limit2 を minlimit と同じ整数のフォームフィールドに変更します。

変更前

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "post": {
        "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"
            }
          },
          {
            "name": "minlimit",
            "in": "formData",
            "description": "min records to return",
            "required": true,
            "type": "integer"
          }
        ],
        "consumes": [
          "application/x-www-form-urlencoded"
        ]
      }
    }
  },
  "parameters": {
    "limitParam": {
      "name": "limit",
      "in": "formData",
      "description": "max records to return",
      "required": true,
      "type": "integer"
    }
  }
}

変更後

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "post": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "name": "limit2",
            "in": "formData",
            "description": "max records to return",
            "required": true,
            "type": "integer"
          },
          {
            "name": "minlimit",
            "in": "formData",
            "description": "min records to return",
            "required": true,
            "type": "integer"
          }
        ],
        "consumes": [
          "application/x-www-form-urlencoded"
        ]
      }
    }
  },
  "parameters": {
    "limitParam": {
      "name": "limit",
      "in": "formData",
      "description": "max records to return",
      "required": true,
      "type": "integer"
    }
  }
}

参考資料