Review documented API responses

How to document the successful responses and errors an operation can return.

Description

An OpenAPI description should document an operation’s actual successful responses and known errors. The same status-code list is not mandatory for every operation; definitions should reflect the actual API contract.

Potential impact

Clients and tests may mishandle errors or expect responses that differ from the server’s behavior.

Remediation

Describe the operation’s successful responses and known errors, including their status codes and any headers or body clients need. In OpenAPI 3.0, a range such as 4XX can cover responses that share a description. Both OpenAPI 2.0 and 3.0 support default for otherwise unlisted status codes.

Examples

This OpenAPI 3.0 excerpt lists selected PUT error responses and OPTIONS responses. It is not a universally required code list; other responses, including successful PUT outcomes, are omitted from this example.

Before

json
{
  "openapi": "3.0.0",
  "paths": {
    "/item": {
      "put": {
        "responses": {
          "default": {
            "description": "Error"
          }
        }
      },
      "options": {
        "responses": {
          "default": {
            "description": "Error"
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "paths": {
    "/item": {
      "put": {
        "responses": {
          "400": { "description": "400 response" },
          "404": { "description": "404 response" },
          "415": { "description": "415 response" },
          "429": { "description": "429 response" },
          "500": { "description": "500 response" }
        }
      },
      "options": {
        "responses": {
          "200": { "description": "200 response" },
          "400": { "description": "400 response" },
          "429": { "description": "429 response" },
          "500": { "description": "500 response" }
        }
      }
    }
  }
}

References