Skip to content

Commit 4aaab77

Browse files
committed
Changes from OAI#2618. Don't merge this commit
1 parent d22f93e commit 4aaab77

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

versions/3.2.0.md

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

23332333
Field Name | Type | Description
23342334
---|:---:|---
2335-
<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.
2335+
<a name="schemaDiscriminator"></a>discriminator | [Discriminator
2336+
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.
23362337
<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.
23372338
<a name="schemaExternalDocs"></a>externalDocs | [External Documentation Object](#externalDocumentationObject) | Additional external documentation for this schema.
23382339
<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.
@@ -2345,13 +2346,8 @@ The OpenAPI Specification allows combining and extending model definitions using
23452346
`allOf` takes an array of object definitions that are validated *independently* but together compose a single object.
23462347

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

23562352
###### XML Modeling
23572353

@@ -2698,9 +2694,14 @@ components:
26982694
26992695
#### <a name="discriminatorObject"></a>Discriminator Object
27002696
2701-
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.
2697+
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` keyword is used to inform the consumer of the document which of the alternatives is expected or preferred.
27022698

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

27052706
##### Fixed Fields
27062707
Field Name | Type | Description
@@ -2722,8 +2723,7 @@ MyResponseType:
27222723
- $ref: '#/components/schemas/Lizard'
27232724
```
27242725

2725-
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:
2726-
2726+
which means the payload _MUST_, by validation, match exactly one of the schemas described by `Cat`, `Dog`, or `Lizard`. Evaluating a `oneOf` can be a costly operation, so `discriminator` MAY be used as a "hint" to improve the efficiency of validation and selection of the matching schema. The `discriminator` keyword can not change the validation result of the `oneOf`, it can only help make the evaluation more efficient and provide better error messaging. We can then describe exactly which field tells us which schema is expected to match the instance:
27272727

27282728
```yaml
27292729
MyResponseType:
@@ -2744,7 +2744,7 @@ The expectation now is that a property with name `petType` _MUST_ be present in
27442744
}
27452745
```
27462746

2747-
Will indicate that the `Cat` schema be used in conjunction with this payload.
2747+
Will indicate that the `Cat` schema is the alternative that is expected to match this payload.
27482748

27492749
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:
27502750

@@ -2762,9 +2762,9 @@ MyResponseType:
27622762
monster: 'https://gigantic-server.com/schemas/Monster/schema.json'
27632763
```
27642764

2765-
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.
2765+
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.
27662766

2767-
When used in conjunction with the `anyOf` construct, the use of the discriminator can avoid ambiguity where multiple schemas may satisfy a single payload.
2767+
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.
27682768

27692769
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.
27702770

@@ -2773,6 +2773,11 @@ For example:
27732773
```yaml
27742774
components:
27752775
schemas:
2776+
MyResponseType:
2777+
oneOf:
2778+
- $ref: '#/components/schemas/Cat'
2779+
- $ref: '#/components/schemas/Dog'
2780+
- $ref: '#/components/schemas/Lizard'
27762781
Pet:
27772782
type: object
27782783
required:
@@ -2810,7 +2815,12 @@ components:
28102815
type: boolean
28112816
```
28122817
2813-
a payload like this:
2818+
The `MyResponseType` schema will use the discriminator defined by the `Pet`
2819+
schema because it is part of the `Cat`, `Dog`, and `Lizard` schemas in the
2820+
`oneOf`. The behavior if not all schemas define a `discriminator` and they are
2821+
not all the same is undefined.
2822+
2823+
Validated against the `MyResponseType` schema, a payload like this:
28142824

28152825
```json
28162826
{
@@ -2819,7 +2829,7 @@ a payload like this:
28192829
}
28202830
```
28212831

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

28242834
```json
28252835
{
@@ -2828,7 +2838,7 @@ will indicate that the `Cat` schema be used. Likewise this schema:
28282838
}
28292839
```
28302840

2831-
will map to `Dog` because of the definition in the `mapping` element.
2841+
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`.
28322842

28332843

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

0 commit comments

Comments
 (0)