Unused header component (OpenAPI 3.0)

A shared header definition is not connected to where it is used

Description

components.headers stores reusable header definitions. Defining a header there does not add it to a response; reference it where it is needed.

Potential impact

Readers may expect a header that the response does not contain, while maintainers keep unnecessary definitions.

Remediation

Use $ref at the actual location, such as a response’s headers, and match the name and schema to the real header. Check external use before removing unnecessary definitions.

Examples

These examples connect X-Pages in the response’s headers to the shared xPages definition.

Before

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": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/MyObject"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    },
    "headers": {
      "xPages": {
        "schema": {
          "type": "integer",
          "description": "number of pages"
        }
      }
    }
  }
}

After

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": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/MyObject"
                  }
                }
              }
            },
            "headers": {
              "X-Pages": {
                "$ref": "#/components/headers/xPages"
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    },
    "headers": {
      "xPages": {
        "schema": {
          "type": "integer",
          "description": "number of pages"
        }
      }
    }
  }
}

References