Designing an OpenAPI discriminator: map polymorphic schema hints
In OpenAPI, discriminator is a hint for choosing among schemas composed with oneOf, anyOf, or allOf. In OAS 3.2.1, propertyName and optional mapping/defaultMapping associate payload values with schemas. The key boundary: discriminator does not change JSON Schema validation. Model the actual payload constraints separately.
In OpenAPI, discriminator is a hint for choosing among schemas composed with oneOf, anyOf, or allOf. In OAS 3.2.1, propertyName and optional mapping/defaultMapping associate payload values with schemas. The key boundary: discriminator does not change JSON Schema validation. Model the actual payload constraints separately.
Use discriminator with oneOf
oneOf means a payload must match exactly one listed schema. propertyName names the field carrying the type value. This example maps cat and dog payload values to explicit schema references.
components:
schemas:
Pet:
type: object
required: [kind]
properties:
kind:
type: string
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: kind
mapping:
cat: '#/components/schemas/Cat'
dog: '#/components/schemas/Dog'
Cat:
type: object
properties:
kind:
const: cat
huntingSkill:
type: string
required: [kind, huntingSkill]
Dog:
type: object
properties:
kind:
const: dog
packSize:
type: integer
minimum: 0
required: [kind, packSize]mapping connects payload values to schema URIs. Without it, implicit schema-name mapping may apply, which couples public values to component names. Explicitly document the public payload values. If kind is cat but huntingSkill is missing, the discriminator hint does not make the payload valid.
What each composition keyword means
| Composition | What the discriminator helps with | Boundary |
|---|---|---|
| oneOf | Select a likely match among alternatives | List all alternatives and satisfy exactly one |
| anyOf | Help consumers choose among schemas that may match | List all alternatives; the hint does not alter validation |
| allOf | Explain parent/child model relationships to consumers | A parent validator does not discover or validate child schemas |
Use discriminator only with oneOf, anyOf, or allOf. With adjacent oneOf/anyOf, list every alternative explicitly. An allOf discriminator is only useful for non-validation purposes; express inheritance/constraints with JSON Schema composition itself.
Mapping and defaults
mapping maps a string in the discriminating property to a component name or URI reference. If public values differ from schema names, state the mapping explicitly. A mapping does not add a schema missing from oneOf/anyOf.
In OAS 3.2.1, if the discriminating property is optional, defaultMapping is required. It identifies the expected schema when the property is absent or has no explicit/implicit mapping. Include that fallback schema among the alternatives and design its validation constraints to accept only intended fallback payloads. Check whether your tools support defaultMapping in the OAS version you use.
Make the contract testable
Decide whether the tag is required or optional.
In oneOf, constrain each alternative's tag using const or enum where appropriate.
Map public values explicitly when they differ from schema names.
If unknown/missing tags are accepted, specify defaultMapping and a validating fallback schema.
Document required and type-specific fields with examples.
Test mapping and default behavior with the actual validator, generator, and runtime toolchain.
This article follows OAS 3.2.1. The example was not run through a schema validator or code generator; check your tools' version-specific support.
Sources
[OpenAPI Specification 3.2.1: Discriminator Object](https://spec.openapis.org/oas/v3.2.1.html#discriminator-object)
[OpenAPI Specification 3.2.1: Schema Composition and Polymorphism](https://spec.openapis.org/oas/v3.2.1.html#composition-and-inheritance-polymorphism)