-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve discriminator * Improve discriminator * Fix unittest * Add unittest * Fix coverage
- Loading branch information
Showing
6 changed files
with
179 additions
and
11 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
37 changes: 37 additions & 0 deletions
37
tests/data/expected/main/main_openapi_discriminator_without_mapping/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_without_mapping.yaml | ||
# timestamp: 2019-07-26T00:00:00+00:00 | ||
|
||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import 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' | ||
my_third_object = 'my_third_object' | ||
|
||
|
||
class ObjectBase(BaseModel): | ||
name: Optional[str] = Field(None, description='Name of the object') | ||
type: Literal['ObjectBase'] = Field(..., description='Object type') | ||
|
||
|
||
class CreateObjectRequest(ObjectBase): | ||
name: str = Field(..., description='Name of the object') | ||
type: Literal['CreateObjectRequest'] = Field(..., description='Object type') | ||
|
||
|
||
class UpdateObjectRequest(ObjectBase): | ||
type: Literal['UpdateObjectRequest'] | ||
|
||
|
||
class Demo(BaseModel): | ||
__root__: 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
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,38 @@ | ||
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 | ||
- my_third_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: | ||
oneOf: | ||
- $ref: "#/components/schemas/ObjectBase" | ||
- $ref: "#/components/schemas/CreateObjectRequest" | ||
- $ref: "#/components/schemas/UpdateObjectRequest" | ||
discriminator: | ||
propertyName: 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