@@ -3212,62 +3443,62 @@
- propertyName |
+propertyName |
string |
-REQUIRED. The name of the property in the payload that will hold the discriminator value. |
+REQUIRED. The name of the property in the payload that will hold the discriminator value. |
- mapping |
+ mapping |
Map[string , string ] |
An object to hold mappings between payload values and schema names or references. |
-This object MAY be extended with Specification Extensions.
+This object MAY be extended with Specification Extensions.
The discriminator object is legal only when using one of the composite keywords oneOf
, anyOf
, allOf
.
-In OAS 3.0, a response payload MAY be described to be exactly one of any number of types:
+In OAS 3.0, a response payload MAY be described to be exactly one of any number of types:
MyResponseType:
oneOf:
- - $ref: '#/components/schemas/Cat'
- - $ref: '#/components/schemas/Dog'
- - $ref: '#/components/schemas/Lizard'
+ - $ref: '#/components/schemas/Cat'
+ - $ref: '#/components/schemas/Dog'
+ - $ref: '#/components/schemas/Lizard'
-which means the payload MUST, by validation, match exactly one of the schemas described by Cat
, Dog
, or Lizard
. In this case, a discriminator MAY act as a “hint” to shortcut validation and selection of the matching schema which may be a costly operation, depending on the complexity of the schema. We can then describe exactly which field tells us which schema to use:
+which means the payload MUST, by validation, match exactly one of the schemas described by Cat
, Dog
, or Lizard
. In this case, a discriminator MAY act as a “hint” to shortcut validation and selection of the matching schema which may be a costly operation, depending on the complexity of the schema. We can then describe exactly which field tells us which schema to use:
MyResponseType:
oneOf:
- - $ref: '#/components/schemas/Cat'
- - $ref: '#/components/schemas/Dog'
- - $ref: '#/components/schemas/Lizard'
+ - $ref: '#/components/schemas/Cat'
+ - $ref: '#/components/schemas/Dog'
+ - $ref: '#/components/schemas/Lizard'
discriminator:
propertyName: petType
-The expectation now is that a property with name petType
MUST be present in the response payload, and the value will correspond to the name of a schema defined in the OAS document. Thus the response payload:
+The expectation now is that a property with name petType
MUST be present in the response payload, and the value will correspond to the name of a schema defined in the OAS document. Thus the response payload:
{
- "id": 12345,
- "petType": "Cat"
+ "id": 12345,
+ "petType": "Cat"
}
Will indicate that the Cat
schema be used in conjunction with this payload.
-In scenarios where the value of the discriminator field does not match the schema name or implicit mapping is not possible, an optional mapping
definition MAY be used:
+In scenarios where the value of the discriminator field does not match the schema name or implicit mapping is not possible, an optional mapping
definition MAY be used:
MyResponseType:
oneOf:
- - $ref: '#/components/schemas/Cat'
- - $ref: '#/components/schemas/Dog'
- - $ref: '#/components/schemas/Lizard'
- - $ref: 'https://gigantic-server.com/schemas/Monster/schema.json'
+ - $ref: '#/components/schemas/Cat'
+ - $ref: '#/components/schemas/Dog'
+ - $ref: '#/components/schemas/Lizard'
+ - $ref: 'https://gigantic-server.com/schemas/Monster/schema.json'
discriminator:
propertyName: petType
mapping:
- dog: '#/components/schemas/Dog'
- monster: 'https://gigantic-server.com/schemas/Monster/schema.json'
+ dog: '#/components/schemas/Dog'
+ monster: 'https://gigantic-server.com/schemas/Monster/schema.json'
-Here the discriminator value of dog
will map to the schema #/components/schemas/Dog
, rather than the default (implicit) value of Dog
. If the discriminator value does not match an implicit or explicit mapping, no schema can be determined and validation SHOULD fail. Mapping keys MUST be string values, but tooling MAY convert response values to strings for comparison.
+Here the discriminator value of dog
will map to the schema #/components/schemas/Dog
, rather than the default (implicit) value of Dog
. If the discriminator value does not match an implicit or explicit mapping, no schema can be determined and validation SHOULD fail. Mapping keys MUST be string values, but tooling MAY convert response values to strings for comparison.
When used in conjunction with the anyOf
construct, the use of the discriminator can avoid ambiguity where multiple schemas may satisfy a single payload.
-In both the oneOf
and anyOf
use cases, all possible schemas MUST be listed explicitly. To avoid redundancy, the discriminator MAY be added to a parent schema definition, and all schemas comprising the parent schema in an allOf
construct may be used as an alternate schema.
+In both the oneOf
and anyOf
use cases, all possible schemas MUST be listed explicitly. To avoid redundancy, the discriminator MAY be added to a parent schema definition, and all schemas comprising the parent schema in an allOf
construct may be used as an alternate schema.
For example:
components:
@@ -3285,7 +3516,7 @@
dog: Dog
Cat:
allOf:
- - $ref: '#/components/schemas/Pet'
+ - $ref: '#/components/schemas/Pet'
- type: object
properties:
@@ -3293,7 +3524,7 @@
type: string
Dog:
allOf:
- - $ref: '#/components/schemas/Pet'
+ - $ref: '#/components/schemas/Pet'
- type: object
properties:
@@ -3301,7 +3532,7 @@
type: string
Lizard:
allOf:
- - $ref: '#/components/schemas/Pet'
+ - $ref: '#/components/schemas/Pet'
- type: object
properties:
@@ -3311,23 +3542,23 @@
a payload like this:
{
- "petType": "Cat",
- "name": "misty"
+ "petType": "Cat",
+ "name": "misty"
}
will indicate that the Cat
schema be used. Likewise this schema:
{
- "petType": "dog",
- "bark": "soft"
+ "petType": "dog",
+ "bark": "soft"
}
will map to Dog
because of the definition in the mapping
element.
-