Skip to content

Commit 7ca63b5

Browse files
authored
Merge pull request #3846 from handrews/disc-of-311
Clarify discriminator + oneOf/anyOf/allOf usage (3.1.1 modified port of #3822)
2 parents d94d13f + dd44da9 commit 7ca63b5

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

Diff for: versions/3.1.1.md

+33-19
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ When parsing an OAD, JSON or YAML objects are parsed into specific Objects (such
191191

192192
If the same JSON/YAML object is parsed multiple times and the respective contexts require it to be parsed as _different_ Object types, the resulting behavior is _implementation defined_, and MAY be treated as an error if detected. An example would be referencing an empty Schema Object under `#/components/schemas` where a Path Item Object is expected, as an empty object is valid for both types. For maximum interoperability, it is RECOMMENDED that OpenAPI Description authors avoid such scenarios.
193193

194+
#### <a name="resolvingImplicitConnections"></a>Resolving Implicit Connections
195+
194196
### <a name="dataTypes"></a>Data Types
195197

196198
Data types in the OAS are based on the types supported by the [JSON Schema Specification Draft 2020-12](https://tools.ietf.org/html/draft-bhutton-json-schema-00#section-4.2.1).
@@ -2706,7 +2708,7 @@ components:
27062708
]
27072709
},
27082710
"Cat": {
2709-
"description": "A representation of a cat. Note that `Cat` will be used as the discriminator value.",
2711+
"description": "A representation of a cat. Note that `Cat` will be used as the discriminating value.",
27102712
"allOf": [
27112713
{
27122714
"$ref": "#/components/schemas/Pet"
@@ -2733,7 +2735,7 @@ components:
27332735
]
27342736
},
27352737
"Dog": {
2736-
"description": "A representation of a dog. Note that `Dog` will be used as the discriminator value.",
2738+
"description": "A representation of a dog. Note that `Dog` will be used as the discriminating value.",
27372739
"allOf": [
27382740
{
27392741
"$ref": "#/components/schemas/Pet"
@@ -2775,7 +2777,7 @@ components:
27752777
required:
27762778
- name
27772779
- petType
2778-
Cat: # "Cat" will be used as the discriminator value
2780+
Cat: # "Cat" will be used as the discriminating value
27792781
description: A representation of a cat
27802782
allOf:
27812783
- $ref: '#/components/schemas/Pet'
@@ -2791,7 +2793,7 @@ components:
27912793
- aggressive
27922794
required:
27932795
- huntingSkill
2794-
Dog: # "Dog" will be used as the discriminator value
2796+
Dog: # "Dog" will be used as the discriminating value
27952797
description: A representation of a dog
27962798
allOf:
27972799
- $ref: '#/components/schemas/Pet'
@@ -2922,24 +2924,38 @@ components:
29222924
29232925
#### <a name="discriminatorObject"></a>Discriminator Object
29242926
2925-
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.
2926-
2927-
`discriminator` uses a schema's "name" to automatically map a property value to
2928-
a schema. The schema's "name" is the property name used when declaring the
2929-
schema as a component in an OpenAPI document. For example, the name of the
2930-
schema at `#/components/schemas/Cat` is "Cat". Therefore, when using
2931-
`discriminator`, _inline_ schemas will not be considered because they don't have
2932-
a "name".
2927+
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.
2928+
This hint can be used to aid in serialization, deserialization, and validation.
2929+
The Discriminator Object does this by implicitly or explicitly associating the possible values of a named property with alternative schemas.
29332930
29342931
##### Fixed Fields
29352932
Field Name | Type | Description
29362933
---|:---:|---
2937-
<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.
2938-
<a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or references.
2934+
<a name="propertyName"></a>propertyName | `string` | **REQUIRED**. The name of the property in the payload that will hold the discriminating value. This property SHOULD be required in the payload schema, as the behavior when the property is absent is undefined.
2935+
<a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or URI references.
29392936

29402937
This object MAY be extended with [Specification Extensions](#specificationExtensions).
29412938

2942-
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.
2939+
##### Conditions for Using the Discriminator Object
2940+
The Discriminator Object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`.
2941+
In both the `oneOf` and `anyOf` use cases, where those keywords are adjacent to `discriminator`, all possible schemas MUST be listed explicitly.
2942+
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.
2943+
2944+
The behavior of any configuration of `oneOf`, `anyOf`, `allOf` and `discriminator` that is not described above is undefined.
2945+
2946+
##### Options for Mapping Values to Schemas
2947+
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.
2948+
The `mapping` entry maps a specific property value to either a different schema component name, or to a schema identified by a URI.
2949+
When using implicit or explicit schema component names, inline `oneOf` or `anyOf` subschemas are not considered.
2950+
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.
2951+
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"`).
2952+
2953+
Mapping keys MUST be string values, but tooling MAY convert response values to strings for comparison.
2954+
However, the exact nature of such conversions are implementation-defined.
2955+
2956+
##### <a name="discriminatorExamples"></a>Examples
2957+
2958+
For these examples, assume all schemas are in the entry OpenAPI document; for handling of `discriminator` in referenced documents see [Resolving Implicit Connections](#resolvingImplicitConnections).
29432959

29442960
In OAS 3.x, a response payload MAY be described to be exactly one of any number of types:
29452961

@@ -2990,13 +3006,11 @@ MyResponseType:
29903006
monster: https://gigantic-server.com/schemas/Monster/schema.json
29913007
```
29923008

2993-
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.
3009+
Here the discriminating value of `dog` will map to the schema `#/components/schemas/Dog`, rather than the default (implicit) value of `#/components/schemas/dog`. If the discriminating value does not match an implicit or explicit mapping, no schema can be determined and validation SHOULD fail.
29943010

29953011
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.
29963012

2997-
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.
2998-
2999-
For example:
3013+
This example shows the `allOf` usage, which avoids needing to reference all child schemas in the parent:
30003014

30013015
```yaml
30023016
components:

0 commit comments

Comments
 (0)