Array without an item limit

Use maxItems to document a maximum length when an array needs an item limit.

Description

An array schema's maxItems defines the maximum number of items. When the service has a practical size limit, document it and enforce the same limit in the implementation.

Potential impact

Processing large arrays without limits can increase server resource usage or make responses difficult for clients to handle.

Remediation

Where the service needs an item limit, set maxItems on the array schema and enforce the same limit on the server. In OpenAPI 2.0, set the limit directly on non-body array parameters. Paginate response arrays where needed.

Examples

The example limits the array to 5 items. Choose a value that matches the actual requirements.

Before

json
{
  "components": {
    "schemas": {
      "GeneralError": {
        "properties": {
          "message": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

After

json
{
  "components": {
    "schemas": {
      "GeneralError": {
        "properties": {
          "message": {
            "type": "array",
            "maxItems": 5,
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

References