Skip to content

Commit 87d023a

Browse files
authored
Merge pull request #3822 from handrews/disc-of-304
Clarify discriminator "*Of" and "mapping" usage (3.0.4)
2 parents 8acc31d + 4a46b20 commit 87d023a

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

Diff for: versions/3.0.4.md

+24-9
Original file line numberDiff line numberDiff line change
@@ -2747,17 +2747,34 @@ components:
27472747
27482748
#### <a name="discriminatorObject"></a>Discriminator Object
27492749
2750-
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 specification of an alternative schema based on the value associated with it.
2751-
2752-
When using the discriminator, _inline_ schemas will not be considered.
2750+
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 by implicitly or explicitly associating the possible values of a named property with alternative schemas.
27532751
27542752
##### Fixed Fields
27552753
Field Name | Type | Description
27562754
---|:---:|---
27572755
<a name="propertyName"></a>propertyName | `string` | **REQUIRED**. The name of the property in the payload that will hold the discriminator value.
2758-
<a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or references.
2756+
<a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or URI references.
2757+
2758+
##### Conditions for Using the Discriminator Object
2759+
The Discriminator Object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`.
2760+
In both the `oneOf` and `anyOf` use cases, where those keywords are adjacent to `discriminator`, all possible schemas MUST be listed explicitly.
2761+
To avoid redundancy, the discriminator MAY be added to a parent schema definition, and all schemas building on the parent schema via an `allOf` construct may be used as an alternate schema.
2762+
It is implementation-defined as to whether all named [Schema Objects](#schemaObject) under the [Components Object](#componentsObject), or only those that are otherwise directly referenced are searched for `allOf` references to the parent schema.
2763+
However, it is RECOMMENDED to search all named schemas in the Components Object because it is common with the `allOf` usage for other parts of the API to only directly reference the parent schema.
2764+
2765+
The behavior of any configuration of `oneOf`, `anyOf`, `allOf` and `discriminator` that is not described above is undefined.
27592766

2760-
The discriminator object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`. Note that because the discriminating property's value is used as a component name and/or as the key in the `mapping` object, the behavior of any value that is not a string is undefined.
2767+
##### Options for Mapping Values to Schemas
2768+
The value of the property named in `propertyName` is used as the name of the associated schema under the [Components Object](#componentsObject), _unless_ a `mapping` is present for that value.
2769+
The `mapping` entry maps a specific property value to either a different schema component name, or to a schema identified by a URI.
2770+
When using implicit or explicit schema component names, inline `oneOf` or `anyOf` subschemas are not considered.
2771+
The behavior of a `mapping` value that is both a valid schema name and a valid relative URI reference is implementation-defined, but it is RECOMMENDED that it be treated as a schema name.
2772+
To ensure that an ambiguous value (e.g. `"foo"`) is treated as a relative URI reference by all implementations, authors MUST prefix it with the `"."` path segment (e.g. `"./foo"`).
2773+
2774+
Mapping keys MUST be string values, but tooling MAY convert response values to strings for comparison.
2775+
However, the exact nature of such conversions are implementation-defined.
2776+
2777+
##### <a name="discriminatorExamples"></a>Examples
27612778

27622779
In OAS 3.0, a response payload MAY be described to be exactly one of any number of types:
27632780

@@ -2809,13 +2826,11 @@ MyResponseType:
28092826
monster: https://gigantic-server.com/schemas/Monster/schema.json
28102827
```
28112828

2812-
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.
2829+
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.
28132830

28142831
When used in conjunction with the `anyOf` construct, the use of the discriminator can avoid ambiguity where multiple schemas may satisfy a single payload.
28152832

2816-
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.
2817-
2818-
For example:
2833+
This example shows the `allOf` usage, which avoids needing to reference all child schemas in the parent:
28192834

28202835
```yaml
28212836
components:

0 commit comments

Comments
 (0)