# schema-discriminator-defined

Source: https://api-genie.dev/docs/linter/openapi/rules/schema-discriminator-defined

A rule of the OpenAPI linter, which reads OpenAPI 3.0, 3.1 and 3.2 documents. Since 0.0.1. Fixed by `--fix`. In the rulesets: Recommended at warning, Strict at error, All at error.

This rule checks that every schema with `oneOf` has a `discriminator`.

## Rule details

Without a discriminator, a tool that reads a value against `oneOf` validates it against every
alternative and keeps the one that matches, which is slow, which fails when two alternatives
match, and which generated code in most languages cannot do at all. With `discriminator`, one
property names the alternative, and a generated client deserializes into the right type.

The rule reports `Schema with oneOf clause does not have a discriminator`.

## Examples

**Good**. `petType` names the alternative.

```yaml
components:
  schemas:
    Pet:
      type: object
      discriminator:
        propertyName: petType
      oneOf:
        - $ref: "#/components/schemas/Cat"
        - $ref: "#/components/schemas/Dog"
```

**Bad**. Whether a pet is a cat or a dog is found by trial.

```yaml
components:
  schemas:
    Pet:
      type: object
      oneOf:
        - $ref: "#/components/schemas/Cat"
        - $ref: "#/components/schemas/Dog"
```

## Automatic fixes

`--fix` adds `discriminator.propertyName` when every alternative of the `oneOf` has one property
in common with a distinct `const` value in each. Every other case stays for you to decide.
