Referencing OpenAPI 2.0, Schema Object, or Swagger 2.0, Schema Object, and the definition of discriminator
field as:
Adds support for polymorphism. The discriminator is the schema property name that is used to differentiate between other schema that inherit this schema. The property name used MUST be defined at this schema and it MUST be in the
required
property list. When used, the value MUST be the name of this schema or any schema that inherits it.
My confusions/ questions:
- It is ambiguous to me, what role exactly it plays in inheritance or polymorphism. Could some one please explain
discriminator
with a working example showing what it exactly does and what if we do not use it? Any errors, warnings or any tools that depends on it for some operations? - Is it the case that swagger-editor does not support
discriminator
, and this field is used in some other tools?
What I have tried so far:
- I have tried to use swagger-editor and the example from the same documentation (also mentioned below), to play around with this property to see if I can see any of its special behaviors. I changed the property, removed it, and extend the
Dog
model to one level deeper and tried the same on the new sub-model, but I did not see any changes in the preview of swagger-editor. - I tried searching online, and specially stackoverflow questions, but did not find any relevant information.
The sample code I used to do experiments:
definitions:
Pet:
type: object
discriminator: petType
properties:
name:
type: string
petType:
type: string
required:
- name
- petType
Cat:
description: A representation of a cat
allOf:
- $ref: '#/definitions/Pet'
- type: object
properties:
huntingSkill:
type: string
description: The measured skill for hunting
default: lazy
enum:
- clueless
- lazy
- adventurous
- aggressive
required:
- huntingSkill
Dog:
description: A representation of a dog
allOf:
- $ref: '#/definitions/Pet'
- type: object
properties:
packSize:
type: integer
format: int32
description: the size of the pack the dog is from
default: 0
minimum: 0
required:
- packSize