-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
artsmolin
committed
Dec 21, 2023
1 parent
26cfde8
commit 58594af
Showing
2 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,35 @@ | ||
# Discriminator | ||
Generate [pydantic classes with discriminators](https://docs.pydantic.dev/latest/api/standard_library_types/#discriminated-unions-aka-tagged-unions) based on [OpenAPI discriminators](https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/). The original OpenAPI specification must have the propertyName and mapping fields. | ||
|
||
|
||
## Example | ||
For OpenAPI schema | ||
```yaml | ||
DiscriminatedOneOfResp: | ||
title: All Of Resp | ||
type: object | ||
required: ['required_discriminated_animal'] | ||
properties: | ||
discriminated_animal: | ||
discriminator: | ||
mapping: {cat: '#/components/schemas/CatWithKind', dog: '#/components/schemas/DogWithKind'} | ||
propertyName: kind | ||
oneOf: | ||
- {$ref: '#/components/schemas/CatWithKind'} | ||
- {$ref: '#/components/schemas/DogWithKind'} | ||
title: Animal | ||
required_discriminated_animal: | ||
discriminator: | ||
mapping: {cat: '#/components/schemas/CatWithKind', dog: '#/components/schemas/DogWithKind'} | ||
propertyName: kind | ||
oneOf: | ||
- {$ref: '#/components/schemas/CatWithKind'} | ||
- {$ref: '#/components/schemas/DogWithKind'} | ||
title: Animal | ||
``` | ||
the following class will be generated | ||
```python | ||
class DiscriminatedOneOfResp(BaseModel): | ||
required_discriminated_animal: CatWithKind | DogWithKind = Field(..., discriminator="kind") | ||
discriminated_animal: CatWithKind | DogWithKind | None = Field(None, discriminator="kind") | ||
``` |
This file contains 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