Linter rulesOpenAPIschema-discriminator-defined
schema-discriminator-defined
Since 0.0.1 · fixed by --fix
This rule ensures that whenever a schema object defines a oneOf clause, it
also defines a discriminator property.
Rule Details
The schema-discriminator-defined rule is designed to ensure that all schema
objects that define a oneOf clause also define the discriminator property.
The discriminator property is used to aid in the deserialization of oneOf
schemas.
Configuration
None.
Examples
Good.
components:
schemas:
Pet:
type: object
discriminator:
propertyName: petType
oneOf:
- $ref: "#/components/schemas/Cat"
- $ref: "#/components/schemas/Dog"
Bad. It is not obvious when a pet is a cat, and when it is a dog.
components:
schemas:
Pet:
type: object
oneOf:
- $ref: "#/components/schemas/Cat"
- $ref: "#/components/schemas/Dog"
When Not to Use It
We recommend to always enable this rule.
Automatic fixes
When invoked with --fix, this rule adds discriminator.propertyName only when
every oneOf alternative exposes one shared property with distinct const
values. All other discriminator choices remain manual.
Compatibility
This rule is compatible with all OpenAPI 3.x versions.