Review discriminator property definitions

Align the discriminator property name with the actual data and schema definition.

Description

A discriminator uses a property value to distinguish schemas for polymorphic data. OpenAPI 3.0 names the property with discriminator.propertyName; OpenAPI 2.0 uses a string discriminator. A mismatch with the actual data or schema definition makes type selection unclear.

Potential impact

Clients and servers may select different subtypes, or generators may be unable to construct the intended models.

Remediation

Verify that the named property is defined in the actual data contract and applicable schema. Check composed and referenced schemas, its required status, and how its values identify the intended schemas.

Examples

These OpenAPI 3.0 excerpts show only properties of a parent schema. Subtypes and composition such as allOf, along with info and paths, are omitted. The first requires petType to be present but does not define its value format.

Before

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "discriminator": {
          "propertyName": "petType"
        },
        "properties": {
          "code": {
            "type": "integer"
          },
          "message": {
            "type": "string"
          }
        },
        "required": [
          "petType"
        ]
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "discriminator": {
          "propertyName": "petType"
        },
        "properties": {
          "code": {
            "type": "integer"
          },
          "message": {
            "type": "string"
          },
          "petType": {
            "type": "string"
          }
        },
        "required": [
          "petType"
        ]
      }
    }
  }
}

The second defines petType as a string property. In the actual polymorphic model, its values must also correspond to the intended subtypes.

References