-
-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix discriminator mapping resolution in schemas with parent-child hie…
…rarchy (#2145) * Add discriminator with properties test * Patch __apply_discriminator_type * Remove unnecessary reference.path check * Fix typing for python3.8 * Remove unnecessary base_class.reference check --------- Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
- Loading branch information
Showing
4 changed files
with
146 additions
and
17 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
36 changes: 36 additions & 0 deletions
36
tests/data/expected/main/openapi/discriminator/discriminator_with_properties.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_with_properties.yaml | ||
# timestamp: 2019-07-26T00:00:00+00:00 | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Literal, Optional, Union | ||
|
||
from pydantic import BaseModel, Field, RootModel | ||
|
||
|
||
class UserContextVariable(BaseModel): | ||
accountId: str = Field(..., description='The account ID of the user.') | ||
type: str = Field(..., description='Type of custom context variable.') | ||
|
||
|
||
class IssueContextVariable(BaseModel): | ||
id: Optional[int] = Field(None, description='The issue ID.') | ||
key: Optional[str] = Field(None, description='The issue key.') | ||
type: str = Field(..., description='Type of custom context variable.') | ||
|
||
|
||
class CustomContextVariable1(UserContextVariable): | ||
type: Literal['user'] = Field(..., description='Type of custom context variable.') | ||
|
||
|
||
class CustomContextVariable2(IssueContextVariable): | ||
type: Literal['issue'] = Field(..., description='Type of custom context variable.') | ||
|
||
|
||
class CustomContextVariable( | ||
RootModel[Union[CustomContextVariable1, CustomContextVariable2]] | ||
): | ||
root: Union[CustomContextVariable1, CustomContextVariable2] = 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,46 @@ | ||
openapi: 3.0.1 | ||
components: | ||
schemas: | ||
CustomContextVariable: | ||
oneOf: | ||
- $ref: '#/components/schemas/UserContextVariable' | ||
- $ref: '#/components/schemas/IssueContextVariable' | ||
properties: | ||
type: | ||
description: Type of custom context variable. | ||
type: string | ||
discriminator: | ||
mapping: | ||
user: '#/components/schemas/UserContextVariable' | ||
issue: '#/components/schemas/IssueContextVariable' | ||
propertyName: type | ||
required: | ||
- type | ||
type: object | ||
UserContextVariable: | ||
properties: | ||
accountId: | ||
description: The account ID of the user. | ||
type: string | ||
type: | ||
description: Type of custom context variable. | ||
type: string | ||
required: | ||
- accountId | ||
- type | ||
type: object | ||
IssueContextVariable: | ||
properties: | ||
id: | ||
description: The issue ID. | ||
format: int64 | ||
type: integer | ||
key: | ||
description: The issue key. | ||
type: string | ||
type: | ||
description: Type of custom context variable. | ||
type: string | ||
required: | ||
- type | ||
type: object |
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