Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discriminator in OpenAPI

I am new to openAPI and trying to understand the discriminator property that can be used along with anyOf and oneOf. how can this property help in implementing the client code?

Please share your wisdom.

like image 689
continuousLearner Avatar asked Sep 01 '25 20:09

continuousLearner


1 Answers

The discriminator is a way to distinguish or "discriminate" different schemas. So let's say you have a schema for Animal but you use OpenAPI's polymorphism capabilities to also define a Cat and Dog. Let's say Animal is "oneOf" Cat or Dog. When the receiver gets a response, how can it determine which one it is?

This is where the discriminator is useful. You can provide a value in a field to Animal that tells you which specific kind of animal it is.

https://spec.openapis.org/oas/v3.1.0#discriminator-object

like image 101
Cristhian Ulloa Avatar answered Sep 03 '25 09:09

Cristhian Ulloa