Acceptヘッダーパラメーター定義の確認

レスポンス形式のネゴシエーションと通常の入力を区別してください。

説明

Acceptは、クライアントが希望するレスポンス形式を伝えるHTTPヘッダーです。OpenAPI 3.0では、この名前の通常のヘッダーパラメーター定義は無視されるため、レスポンスのメディアタイプで契約を記述してください。

想定される影響

定義したパラメーターがドキュメントツールに反映されなかったり、業務上の入力と形式のネゴシエーションが混同されたりする場合があります。

対処方法

レスポンス形式はOpenAPI 3.0のレスポンスのcontent、またはSwagger 2.0のproducesで記述してください。別の入力が実際に必要なら適切なパラメーターとして定義し、サーバーとクライアントの契約を確認してください。HTTPのAcceptヘッダー自体を使わないという意味ではありません。

例

OpenAPI 3.0でID入力の名前と場所を比較する抜粋です。/users/{id}の独立したパスパラメーター定義は省略しています。

変更前

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": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      },
      "parameters": [
        {
          "name": "Accept",
          "in": "header",
          "description": "ID of the API the version",
          "required": true,
          "schema": {
            "type": "integer"
          }
        }
      ]
    },
    "/users/{id}": {
      "get": {
        "parameters": [
          {
            "in": "header",
            "name": "Accept",
            "required": true,
            "description": "The user ID",
            "schema": {
              "type": "integer",
              "minimum": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      }
    }
  }
}

変更後

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": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      },
      "parameters": [
        {
          "name": "id",
          "in": "query",
          "description": "ID of the API the version",
          "required": true,
          "schema": {
            "type": "integer"
          }
        }
      ]
    },
    "/users/{id}": {
      "get": {
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "in": "header",
            "name": "id",
            "required": true,
            "description": "The user ID",
            "schema": {
              "type": "integer",
              "minimum": 1
            }
          }
        ]
      }
    }
  }
}

変更後のID入力はqueryまたは別のheaderパラメーターです。レスポンス形式のネゴシエーションを置き換えるものではなく、実際のAPIもその入力位置を扱う必要があります。

参考資料