Why Does My oneOf Schema Cause Deserialization Errors?

The Problem

You have a schema with multiple object types:

oneOf:
  - $ref: '#/components/schemas/Dog'
  - $ref: '#/components/schemas/Cat'

When receiving JSON, the deserializer doesn't know which schema to use. It must:

  1. Try deserializing as Dog
  2. If that fails, try Cat
  3. If all fail, throw an error

The Fix

Add a discriminator field that explicitly identifies which schema to use:

oneOf:
  - $ref: '#/components/schemas/Dog'
  - $ref: '#/components/schemas/Cat'
discriminator:
  propertyName: petType
  mapping:
    dog: '#/components/schemas/Dog'
    cat: '#/components/schemas/Cat'

Now the deserializer looks at the petType field in the JSON and knows immediately which schema to use.

Catch All Silent Failures

Use our OpenAPI Silent Failure Validator to find this and other breaking patterns.

An unhandled error has occurred. Reload