Review direct self-references in schema composition

Review references that repeatedly apply a schema to the same data and distinguish intentional recursive models.

Description

An allOf that points back to its own schema can make tools process the same value repeatedly.

Potential impact

Schema validation can slow down, and documentation or code generation can fail.

Remediation

Remove the direct self-reference or move shared fields into a separate schema. If recursion is intentional, as with a tree’s child nodes, check that your tools handle it and that the model still accepts the intended data.

Examples

In the first excerpt, ExtendedErrorModel refers to itself inside allOf.

Before

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "ExtendedErrorModel": {
        "allOf": [
          {
            "$ref": "#/components/schemas/ExtendedErrorModel"
          },
          {
            "type": "object",
            "properties": {
              "rootCause": {
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "ErrorModel": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string"
          }
        }
      },
      "ExtendedErrorModel": {
        "allOf": [
          {
            "$ref": "#/components/schemas/ErrorModel"
          },
          {
            "type": "object",
            "properties": {
              "rootCause": {
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}

The revision references ErrorModel, which defines message, and keeps rootCause on the extended model. Check that both fields belong in your error response.

References