Skip to content

Commit 0bb7541

Browse files
committed
More discriminator guidance consolidation
This creates subsections to organize the different topics, pulls key guidance out of the examples and up into those sections, and provides clarification on the ambiguity of names and URIs.
1 parent 7e3b58b commit 0bb7541

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

versions/3.0.4.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,21 +2734,35 @@ components:
27342734
27352735
#### <a name="discriminatorObject"></a>Discriminator Object
27362736
2737-
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.
2738-
2739-
When using the discriminator, _inline_ schemas will not be considered.
2737+
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.
2738+
It does this by implicitly or explicitly associating the possible values of a named property with alternative schemas.
27402739
27412740
##### Fixed Fields
27422741
Field Name | Type | Description
27432742
---|:---:|---
27442743
<a name="propertyName"></a>propertyName | `string` | **REQUIRED**. The name of the property in the payload that will hold the discriminator value.
2745-
<a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or references.
2744+
<a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or URI references.
27462745

2747-
The discriminator object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`.
2746+
##### Conditions for Using the Discriminator Object
2747+
The Discriminator Object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`.
27482748
In both the `oneOf` and `anyOf` use cases, where those keywords are adjacent to `discriminator`, all possible schemas MUST be listed explicitly.
27492749
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.
2750-
The behavior of any other configuration of `oneOf`, `anyOf`, `allOf` and `discriminator` is undefined.
2751-
Note also 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.
2750+
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 matching `allOf` references.
2751+
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 (that contains the `discriminator`).
2752+
2753+
The behavior of any configuration of `oneOf`, `anyOf`, `allOf` and `discriminator` that is not described above is undefined.
2754+
2755+
##### Options for Mapping Values to Schemas
2756+
The value of the property named in `propertyName` is is used as the name of the associated schema under the [Components Object](#componentsObject), _unless_ a `mapping` is present for that value.
2757+
The `mapping` entry maps a specific property value to either a different schema component name, or to a schema identified by a URI.
2758+
When using implicit or explicit schema component names, inline `oneOf` or `anyOf` subschemas are not considered.
2759+
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.
2760+
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"`).
2761+
2762+
Mapping keys MUST be string values, but tooling MAY convert response values to strings for comparison.
2763+
However, the exact nature of such conversions are implementation-defined.
2764+
2765+
##### <a name="discriminatorExamples"></a>Examples
27522766

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

@@ -2800,7 +2814,7 @@ MyResponseType:
28002814
monster: https://gigantic-server.com/schemas/Monster/schema.json
28012815
```
28022816

2803-
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.
2817+
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.
28042818

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

0 commit comments

Comments
 (0)