Skip to content

Commit 0a8b3f7

Browse files
committed
documentation
1 parent 9071de0 commit 0a8b3f7

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

.changeset/discriminators.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
default: minor
3+
---
4+
5+
# Add discriminator property support
6+
7+
The optional `discriminator` field, when used in a schema with `anyOf` or `oneOf` as described in [OpenAPI 3.1.0](https://spec.openapis.org/oas/v3.1.0.html#discriminator-object), now correctly produces deserialization logic for using the specified property value to determine the appropriate type.
8+
9+
In this example, `PolymorphicModel.thing` will be deserialized as a `ThingA` if the value of the `modelType` property is `"ThingA"`, or as a `ThingB` if the value is `"ThingB"`:
10+
11+
```yaml
12+
ThingA:
13+
type: object
14+
properties:
15+
thingType:
16+
type: string
17+
name:
18+
type: string
19+
20+
ThingB:
21+
type: object
22+
properties:
23+
thingType:
24+
type: string
25+
name:
26+
type: string
27+
28+
PolymorphicModel:
29+
type: object
30+
properties:
31+
thing:
32+
anyOf:
33+
- "#/components/schemas/ThingA"
34+
- "#/components/schemas/ThingB"
35+
discriminator:
36+
propertyName: modelType
37+
```
38+
39+
If you want to use property values that are not the same as the schema names, you can add a `mapping`. In this example, the value is expected to be `"A"` or `"B"`, instead of `"ThingA"` or `"ThingB"`:
40+
41+
```yaml
42+
discriminator:
43+
propertyName: modelType
44+
mapping:
45+
A: "#/components/schemas/ThingA"
46+
B: "#/components/schemas/ThingB"
47+
```
48+
49+
This could also be written more concisely as:
50+
51+
```yaml
52+
discriminator:
53+
propertyName: modelType
54+
mapping:
55+
A: "ThingA"
56+
B: "ThingB"
57+
```
58+
59+
If you specify a property name that does not exist in all of the variant schemas, the behavior is undefined.

0 commit comments

Comments
 (0)