Skip to content

Commit 031aea8

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 eae07a8 commit 031aea8

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

versions/3.1.1.md

+5-6
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` field.
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
As such, that property MUST be a required field.
23522352
There are two ways to define the value of a discriminator for an inheriting instance.
23532353
- Use the schema name.
@@ -2727,8 +2727,7 @@ MyResponseType:
27272727
- $ref: '#/components/schemas/Lizard'
27282728
```
27292729

2730-
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:
2731-
2730+
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:
27322731

27332732
```yaml
27342733
MyResponseType:
@@ -2749,7 +2748,7 @@ The expectation now is that a property with name `petType` _MUST_ be present in
27492748
}
27502749
```
27512750

2752-
Will indicate that the `Cat` schema be used in conjunction with this payload.
2751+
Will indicate that the `Cat` schema is the alternative that is expected to match this payload.
27532752

27542753
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:
27552754

@@ -2769,7 +2768,7 @@ MyResponseType:
27692768

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

2772-
When used in conjunction with the `anyOf` construct, the use of the discriminator can avoid ambiguity where multiple schemas may satisfy a single payload.
2771+
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.
27732772

27742773
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.
27752774

@@ -2824,7 +2823,7 @@ a payload like this:
28242823
}
28252824
```
28262825

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

28292828
```json
28302829
{

0 commit comments

Comments
 (0)