Skip to content

Commit 314a756

Browse files
committed
Discriminator: Can't be a hint and a shortcut
The `discriminator` keyword can't be a hint and a shortcut. I tried here to modify any language that might suggest that `discriminator` overrides the behavior of `anyOf`/`oneOf` in any way.
1 parent bde8f97 commit 314a756

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

versions/3.1.1.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,7 @@ The OpenAPI Specification allows combining and extending model definitions using
23472347

23482348
While composition offers model extensibility, it does not imply a hierarchy between the models.
23492349
To support polymorphism, the OpenAPI Specification adds the `discriminator` keyword.
2350-
When used, the `discriminator` will indicate the name of the property that decides which schema definition validates the structure of the model.
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.
23512351

23522352
###### XML Modeling
23532353

@@ -2694,7 +2694,7 @@ components:
26942694
26952695
#### <a name="discriminatorObject"></a>Discriminator Object
26962696
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 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.
26982698

26992699
`discriminator` uses a schema's "name" to automatically map a property value to
27002700
a schema. The schema's "name" is the property name used when declaring the
@@ -2722,8 +2722,7 @@ MyResponseType:
27222722
- $ref: '#/components/schemas/Lizard'
27232723
```
27242724

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-
2725+
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:
27272726

27282727
```yaml
27292728
MyResponseType:
@@ -2744,7 +2743,7 @@ The expectation now is that a property with name `petType` _MUST_ be present in
27442743
}
27452744
```
27462745

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

27492748
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:
27502749

@@ -2764,7 +2763,7 @@ MyResponseType:
27642763

27652764
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.
27662765

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.
2766+
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.
27682767

27692768
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.
27702769

@@ -2819,7 +2818,7 @@ a payload like this:
28192818
}
28202819
```
28212820

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

28242823
```json
28252824
{

0 commit comments

Comments
 (0)