Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 9.2k
Clarify discriminator + oneOf/anyOf/allOf usage (3.1.1 modified port of #3822)#3846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -191,6 +191,8 @@ When parsing an OAD, JSON or YAML objects are parsed into specific Objects (such | ||
| 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. | ||
| #### <a name="resolvingImplicitConnections"></a>Resolving Implicit Connections | ||
| ### <a name="dataTypes"></a>Data Types | ||
| 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: | ||
| ] | ||
| }, | ||
| "Cat": { | ||
| "description": "A representation of a cat. Note that `Cat` will be used as the discriminator value.", | ||
| "description": "A representation of a cat. Note that `Cat` will be used as the discriminating value.", | ||
| "allOf": [ | ||
| { | ||
| "$ref": "#/components/schemas/Pet" | ||
| @@ -2733,7 +2735,7 @@ components: | ||
| ] | ||
| }, | ||
| "Dog": { | ||
| "description": "A representation of a dog. Note that `Dog` will be used as the discriminator value.", | ||
| "description": "A representation of a dog. Note that `Dog` will be used as the discriminating value.", | ||
| "allOf": [ | ||
| { | ||
| "$ref": "#/components/schemas/Pet" | ||
| @@ -2775,7 +2777,7 @@ components: | ||
| required: | ||
| - name | ||
| - petType | ||
| Cat: # "Cat" will be used as the discriminator value | ||
| Cat: # "Cat" will be used as the discriminating value | ||
| description: A representation of a cat | ||
| allOf: | ||
| - $ref: '#/components/schemas/Pet' | ||
| @@ -2791,7 +2793,7 @@ components: | ||
| - aggressive | ||
| required: | ||
| - huntingSkill | ||
| Dog: # "Dog" will be used as the discriminator value | ||
| Dog: # "Dog" will be used as the discriminating value | ||
| description: A representation of a dog | ||
| allOf: | ||
| - $ref: '#/components/schemas/Pet' | ||
| @@ -2922,24 +2924,38 @@ components: | ||
| #### <a name="discriminatorObject"></a>Discriminator Object | ||
| 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. | ||
| `discriminator` uses a schema's "name" to automatically map a property value to | ||
| a schema. The schema's "name" is the property name used when declaring the | ||
| schema as a component in an OpenAPI document. For example, the name of the | ||
| schema at `#/components/schemas/Cat` is "Cat". Therefore, when using | ||
| `discriminator`, _inline_ schemas will not be considered because they don't have | ||
| a "name". | ||
| 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. | ||
| This hint can be used to aid in serialization, deserialization, and validation. | ||
| The Discriminator Object does this by implicitly or explicitly associating the possible values of a named property with alternative schemas. | ||
ralfhandl marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ##### Fixed Fields | ||
| Field Name | Type | Description | ||
| ---|:---:|--- | ||
| <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. | ||
| <a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or references. | ||
| <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. | ||
| <a name="discriminatorMapping"></a> mapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or URI references. | ||
| This object MAY be extended with [Specification Extensions](#specificationExtensions). | ||
| 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. | ||
| ##### Conditions for Using the Discriminator Object | ||
| The Discriminator Object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`. | ||
| In both the `oneOf` and `anyOf` use cases, where those keywords are adjacent to `discriminator`, all possible schemas MUST be listed explicitly. | ||
| 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. | ||
| The behavior of any configuration of `oneOf`, `anyOf`, `allOf` and `discriminator` that is not described above is undefined. | ||
| ##### Options for Mapping Values to Schemas | ||
| 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. | ||
| The `mapping` entry maps a specific property value to either a different schema component name, or to a schema identified by a URI. | ||
| When using implicit or explicit schema component names, inline `oneOf` or `anyOf` subschemas are not considered. | ||
| 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. | ||
| 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"`). | ||
| Mapping keys MUST be string values, but tooling MAY convert response values to strings for comparison. | ||
| However, the exact nature of such conversions are implementation-defined. | ||
| ##### <a name="discriminatorExamples"></a>Examples | ||
| For these examples, assume all schemas are in the entry OpenAPI document; for handling of `discriminator` in referenced documents see [Resolving Implicit Connections](#resolvingImplicitConnections). | ||
| In OAS 3.x, a response payload MAY be described to be exactly one of any number of types: | ||
| @@ -2990,13 +3006,11 @@ MyResponseType: | ||
| monster: https://gigantic-server.com/schemas/Monster/schema.json | ||
| ``` | ||
| 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. | ||
| 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. | ||
| 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. | ||
| 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. | ||
| For example: | ||
| This example shows the `allOf` usage, which avoids needing to reference all child schemas in the parent: | ||
| ```yaml | ||
| components: | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like an empty H4 ... is that what was intended? It looks very odd.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to keep the markdown validator from complaining about broken links. That section is being added in another PR. I have like 6 different PRs that cross-reference each other and there's no good way to do this. Of the three ways I've tried, this was the only one that @ralfhandl didn't object to.
It will all get sorted as we merge and resolve conflicts.