Skip to content

Commit 32fd487

Browse files
authored
Merge pull request #2618 from jdesrosiers/311-discriminator-fixes
3.1.1 discriminator improvements
2 parents 22aef73 + 2d940c5 commit 32fd487

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

versions/3.1.1.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,7 +2330,7 @@ The OpenAPI Specification's base vocabulary is comprised of the following keywor
23302330

23312331
Field Name | Type | Description
23322332
---|:---:|---
2333-
<a name="schemaDiscriminator"></a>discriminator | [Discriminator Object](#discriminatorObject) | Adds support for polymorphism. The discriminator is an object name that is used to differentiate between other schemas which may satisfy the payload description. See [Composition and Inheritance](#schemaComposition) for more details.
2333+
<a name="schemaDiscriminator"></a>discriminator | [Discriminator Object](#discriminatorObject) | Adds support for polymorphism. The discriminator is used to determine which of a set of schemas a payload is expected to satisfy. See [Composition and Inheritance](#schemaComposition) for more details.
23342334
<a name="schemaXml"></a>xml | [XML Object](#xmlObject) | This MAY be used only on properties schemas. It has no effect on root schemas. Adds additional metadata to describe the XML representation of this property.
23352335
<a name="schemaExternalDocs"></a>externalDocs | [External Documentation Object](#externalDocumentationObject) | Additional external documentation for this schema.
23362336
<a name="schemaExample"></a>example | Any | A free-form property to include an example of an instance for this schema. To represent examples that cannot be naturally represented in JSON or YAML, a string value can be used to contain the example with escaping where necessary.<br><br>**Deprecated:** The `example` property has been deprecated in favor of the JSON Schema `examples` keyword. Use of `example` is discouraged, and later versions of this specification may remove it.
@@ -2343,13 +2343,8 @@ The OpenAPI Specification allows combining and extending model definitions using
23432343
`allOf` takes an array of object definitions that are validated *independently* but together compose a single object.
23442344

23452345
While composition offers model extensibility, it does not imply a hierarchy between the models.
2346-
To support polymorphism, the OpenAPI Specification adds the `discriminator` field.
2347-
When used, the `discriminator` will be the name of the property that decides which schema definition validates the structure of the model.
2348-
As such, the `discriminator` field MUST be a required field.
2349-
There are two ways to define the value of a discriminator for an inheriting instance.
2350-
- Use the schema name.
2351-
- Override the schema name by overriding the property with a new value. If a new value exists, this takes precedence over the schema name.
2352-
As such, inline schema definitions, which do not have a given id, *cannot* be used in polymorphism.
2346+
To support polymorphism, the OpenAPI Specification adds the `discriminator` keyword.
2347+
When used, the `discriminator` indicates the name of the property that hints which schema definition is expected to validate the structure of the model.
23532348

23542349
###### XML Modeling
23552350

@@ -2696,14 +2691,19 @@ components:
26962691
26972692
#### <a name="discriminatorObject"></a>Discriminator Object
26982693
2699-
When request bodies or response payloads may be one of a number of different schemas, a `discriminator` object can be used to aid in serialization, deserialization, and validation. The discriminator is a specific object in a schema which is used to inform the consumer of the document of an alternative schema based on the value associated with it.
2694+
When request bodies or response payloads may be one of a number of different schemas, a `discriminator` object gives a hint about the expected schema of the document. It can be used to aid in serialization, deserialization, and validation.
27002695

2701-
When using the discriminator, _inline_ schemas will not be considered.
2696+
`discriminator` uses a schema's "name" to automatically map a property value to
2697+
a schema. The schema's "name" is the property name used when declaring the
2698+
schema as a component in an OpenAPI document. For example, the name of the
2699+
schema at `#/components/schemas/Cat` is "Cat". Therefore, when using
2700+
`discriminator`, _inline_ schemas will not be considered because they don't have
2701+
a "name".
27022702

27032703
##### Fixed Fields
27042704
Field Name | Type | Description
27052705
---|:---:|---
2706-
<a name="propertyName"></a>propertyName | `string` | **REQUIRED**. The name of the property in the payload that will hold the discriminator value.
2706+
<a name="propertyName"></a>propertyName | `string` | **REQUIRED**. The name of the property in the payload that will hold the discriminator value. This property MUST be required in the payload schema.
27072707
<a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or references.
27082708

27092709
This object MAY be extended with [Specification Extensions](#specificationExtensions).
@@ -2720,8 +2720,7 @@ MyResponseType:
27202720
- $ref: '#/components/schemas/Lizard'
27212721
```
27222722

2723-
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:
2724-
2723+
which means the payload _MUST_, by validation, match exactly one of the schemas described by `Cat`, `Dog`, or `Lizard`. Deserialization of a `oneOf` can be a costly operation, as it requires determining which schema matches the payload and thus should be used in deserialization. This problem also exists for `anyOf` schemas. A `discriminator` MAY be used as a "hint" to improve the efficiency of selection of the matching schema. The `discriminator` keyword cannot change the validation result of the `oneOf`, it can only help make the deserialization more efficient and provide better error messaging. We can specify the exact field that tells us which schema is expected to match the instance:
27252724

27262725
```yaml
27272726
MyResponseType:
@@ -2742,7 +2741,7 @@ The expectation now is that a property with name `petType` _MUST_ be present in
27422741
}
27432742
```
27442743

2745-
Will indicate that the `Cat` schema be used in conjunction with this payload.
2744+
Will indicate that the `Cat` schema is expected to match this payload.
27462745

27472746
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:
27482747

@@ -2760,9 +2759,9 @@ MyResponseType:
27602759
monster: https://gigantic-server.com/schemas/Monster/schema.json
27612760
```
27622761

2763-
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.
2762+
Here the discriminator _value_ of `dog` will map to the schema `#/components/schemas/Dog`, rather than the default (implicit) `#/components/schemas/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.
27642763

2765-
When used in conjunction with the `anyOf` construct, the use of the discriminator can avoid ambiguity where multiple schemas may satisfy a single payload.
2764+
When used in conjunction with the `anyOf` construct, the use of the discriminator can avoid ambiguity for serializers/deserializers where multiple schemas may satisfy a single payload.
27662765

27672766
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.
27682767

@@ -2808,7 +2807,7 @@ components:
28082807
type: boolean
28092808
```
28102809
2811-
a payload like this:
2810+
Validated against the `Pet` schema, a payload like this:
28122811

28132812
```json
28142813
{
@@ -2817,7 +2816,7 @@ a payload like this:
28172816
}
28182817
```
28192818

2820-
will indicate that the `Cat` schema be used. Likewise this schema:
2819+
will indicate that the `#/components/schemas/Cat` schema is expected to match. Likewise this payload:
28212820

28222821
```json
28232822
{
@@ -2826,7 +2825,7 @@ will indicate that the `Cat` schema be used. Likewise this schema:
28262825
}
28272826
```
28282827

2829-
will map to `Dog` because of the definition in the `mapping` element.
2828+
will map to `#/components/schemas/Dog` because the `dog` entry in the `mapping` element maps to `Dog` which is the schema name for `#/components/schemas/Dog`.
28302829

28312830

28322831
#### <a name="xmlObject"></a>XML Object

0 commit comments

Comments
 (0)