-
-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make discriminators work with multiple keys pointing to the same schema
- Loading branch information
Showing
4 changed files
with
141 additions
and
7 deletions.
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
37 changes: 37 additions & 0 deletions
37
tests/data/expected/main/main_openapi_discriminator_enum_duplicate/output.py
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# generated by datamodel-codegen: | ||
# filename: discriminator_enum_duplicate.yaml | ||
# timestamp: 2019-07-26T00:00:00+00:00 | ||
|
||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import Literal, Optional, Union | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class Cat(BaseModel): | ||
pet_type: Literal['cat'] = Field(..., title='Pet Type') | ||
meows: int = Field(..., title='Meows') | ||
|
||
|
||
class Dog(BaseModel): | ||
pet_type: Literal['dog'] = Field(..., title='Pet Type') | ||
barks: float = Field(..., title='Barks') | ||
|
||
|
||
class PetType(Enum): | ||
reptile = 'reptile' | ||
lizard = 'lizard' | ||
|
||
|
||
class Lizard(BaseModel): | ||
pet_type: Literal['lizard', 'reptile'] = Field(..., title='Pet Type') | ||
scales: bool = Field(..., title='Scales') | ||
|
||
|
||
class Animal(BaseModel): | ||
pet: Optional[Union[Cat, Dog, Lizard]] = Field( | ||
None, discriminator='pet_type', title='Pet' | ||
) | ||
n: Optional[int] = Field(None, title='N') |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Example from https://docs.pydantic.dev/latest/concepts/unions/#discriminated-unions | ||
openapi: 3.1.0 | ||
components: | ||
schemas: | ||
Cat: | ||
properties: | ||
pet_type: | ||
const: "cat" | ||
title: "Pet Type" | ||
meows: | ||
title: Meows | ||
type: integer | ||
required: | ||
- pet_type | ||
- meows | ||
title: Cat | ||
type: object | ||
Dog: | ||
properties: | ||
pet_type: | ||
const: "dog" | ||
title: "Pet Type" | ||
barks: | ||
title: Barks | ||
type: number | ||
required: | ||
- pet_type | ||
- barks | ||
title: Dog | ||
type: object | ||
Lizard: | ||
properties: | ||
pet_type: | ||
enum: | ||
- reptile | ||
- lizard | ||
title: Pet Type | ||
type: string | ||
scales: | ||
title: Scales | ||
type: boolean | ||
required: | ||
- pet_type | ||
- scales | ||
title: Lizard | ||
type: object | ||
Animal: | ||
properties: | ||
pet: | ||
discriminator: | ||
mapping: | ||
cat: '#/components/schemas/Cat' | ||
dog: '#/components/schemas/Dog' | ||
lizard: '#/components/schemas/Lizard' | ||
reptile: '#/components/schemas/Lizard' | ||
propertyName: pet_type | ||
oneOf: | ||
- $ref: '#/components/schemas/Cat' | ||
- $ref: '#/components/schemas/Dog' | ||
- $ref: '#/components/schemas/Lizard' | ||
title: Pet | ||
'n': | ||
title: 'N' | ||
type: integer |
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