-
-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support discriminators in array items (#1458)
* setup usecase * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Support discriminator in array * Support discriminator in array --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Koudai Aono <[email protected]>
- Loading branch information
1 parent
a31148d
commit 5ccc441
Showing
7 changed files
with
228 additions
and
2 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
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
40 changes: 40 additions & 0 deletions
40
tests/data/expected/main/main_openapi_discriminator_in_array/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,40 @@ | ||
# generated by datamodel-codegen: | ||
# filename: discriminator_in_array.yaml | ||
# timestamp: 2023-07-27T00:00:00+00:00 | ||
|
||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import List, Optional, Union | ||
|
||
from pydantic import BaseModel, Field | ||
from typing_extensions import Literal | ||
|
||
|
||
class Type(Enum): | ||
my_first_object = 'my_first_object' | ||
my_second_object = 'my_second_object' | ||
|
||
|
||
class ObjectBase(BaseModel): | ||
name: Optional[str] = Field(None, description='Name of the object') | ||
type: Literal['type1'] = Field(..., description='Object type') | ||
|
||
|
||
class CreateObjectRequest(ObjectBase): | ||
name: str = Field(..., description='Name of the object') | ||
type: Literal['type2'] = Field(..., description='Object type') | ||
|
||
|
||
class UpdateObjectRequest(ObjectBase): | ||
type: Literal['type3'] | ||
|
||
|
||
class MyArray(BaseModel): | ||
__root__: Union[ObjectBase, CreateObjectRequest, UpdateObjectRequest] = Field( | ||
..., discriminator='type' | ||
) | ||
|
||
|
||
class Demo(BaseModel): | ||
myArray: List[MyArray] |
36 changes: 36 additions & 0 deletions
36
tests/data/expected/main/main_openapi_discriminator_in_array_collapse_root_models/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,36 @@ | ||
# generated by datamodel-codegen: | ||
# filename: discriminator_in_array.yaml | ||
# timestamp: 2023-07-27T00:00:00+00:00 | ||
|
||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import List, Optional, Union | ||
|
||
from pydantic import BaseModel, Field | ||
from typing_extensions import Literal | ||
|
||
|
||
class Type(Enum): | ||
my_first_object = 'my_first_object' | ||
my_second_object = 'my_second_object' | ||
|
||
|
||
class ObjectBase(BaseModel): | ||
name: Optional[str] = Field(None, description='Name of the object') | ||
type: Literal['type1'] = Field(..., description='Object type') | ||
|
||
|
||
class CreateObjectRequest(ObjectBase): | ||
name: str = Field(..., description='Name of the object') | ||
type: Literal['type2'] = Field(..., description='Object type') | ||
|
||
|
||
class UpdateObjectRequest(ObjectBase): | ||
type: Literal['type3'] | ||
|
||
|
||
class Demo(BaseModel): | ||
myArray: List[Union[ObjectBase, CreateObjectRequest, UpdateObjectRequest]] = Field( | ||
..., discriminator='type' | ||
) |
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,48 @@ | ||
openapi: "3.0.0" | ||
components: | ||
schemas: | ||
ObjectBase: | ||
description: Object schema | ||
type: object | ||
properties: | ||
name: | ||
description: Name of the object | ||
type: string | ||
type: | ||
description: Object type | ||
type: string | ||
enum: | ||
- my_first_object | ||
- my_second_object | ||
CreateObjectRequest: | ||
description: Request schema for object creation | ||
type: object | ||
allOf: | ||
- $ref: '#/components/schemas/ObjectBase' | ||
required: | ||
- name | ||
- type | ||
UpdateObjectRequest: | ||
description: Request schema for object updates | ||
type: object | ||
allOf: | ||
- $ref: '#/components/schemas/ObjectBase' | ||
Demo: | ||
type: object | ||
required: | ||
- myArray | ||
properties: | ||
myArray: | ||
type: array | ||
items: | ||
oneOf: | ||
- $ref: "#/components/schemas/ObjectBase" | ||
- $ref: "#/components/schemas/CreateObjectRequest" | ||
- $ref: "#/components/schemas/UpdateObjectRequest" | ||
discriminator: | ||
propertyName: type | ||
mapping: | ||
type1: "#/components/schemas/ObjectBase" | ||
type2: "#/components/schemas/CreateObjectRequest" | ||
type3: "#/components/schemas/UpdateObjectRequest" | ||
|
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,48 @@ | ||
openapi: "3.0.0" | ||
components: | ||
schemas: | ||
ObjectBase: | ||
description: Object schema | ||
type: object | ||
properties: | ||
name: | ||
description: Name of the object | ||
type: string | ||
type: | ||
description: Object type | ||
type: string | ||
enum: | ||
- my_first_object | ||
- my_second_object | ||
CreateObjectRequest: | ||
description: Request schema for object creation | ||
type: object | ||
allOf: | ||
- $ref: '#/components/schemas/ObjectBase' | ||
required: | ||
- name | ||
- type | ||
UpdateObjectRequest: | ||
description: Request schema for object updates | ||
type: object | ||
allOf: | ||
- $ref: '#/components/schemas/ObjectBase' | ||
Demo: | ||
type: object | ||
required: | ||
- myArray | ||
properties: | ||
myArray: | ||
type: array | ||
items: | ||
anyOf: | ||
- $ref: "#/components/schemas/ObjectBase" | ||
- $ref: "#/components/schemas/CreateObjectRequest" | ||
- $ref: "#/components/schemas/UpdateObjectRequest" | ||
discriminator: | ||
propertyName: type | ||
mapping: | ||
type1: "#/components/schemas/ObjectBase" | ||
type2: "#/components/schemas/CreateObjectRequest" | ||
type3: "#/components/schemas/UpdateObjectRequest" | ||
|
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