Maximum string length is undefined (OpenAPI 3.0)

A string that needs a length limit lacks maxLength

Description

A string field can accept very long values unless its schema limits their length.

Potential impact

If the server accepts those values, processing or storing them can exhaust resources or cause errors.

Remediation

If your API limits a field’s length, enforce the limit on the server. Set maxLength for that field unless its enum already restricts strings to the allowed length.

Examples

The example allows up to 15 characters for code and 255 for message.

Before

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string"
          },
          "message": {
            "type": "string"
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string",
            "maxLength": 15
          },
          "message": {
            "type": "string",
            "maxLength": 255
          }
        }
      }
    }
  }
}

References