-
-
Notifications
You must be signed in to change notification settings - Fork 449
Fix optional discriminator literals #3608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
koxudaxi
wants to merge
3
commits into
main
Choose a base branch
from
agent/fix-optional-discriminator-literals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
tests/data/expected/dynamic_models/force_optional_discriminator_literals.json
This file contains hidden or 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,14 @@ | ||
| { | ||
| "CardPayment": { | ||
| "kind": "card", | ||
| "card_number": null | ||
| }, | ||
| "CashPayment": { | ||
| "kind": "cash", | ||
| "received_amount": null | ||
| }, | ||
| "Payment": { | ||
| "kind": "cash", | ||
| "received_amount": 50 | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...ta/expected/main/openapi/discriminator/enum_single_value_anyof_use_enum_force_optional.py
This file contains hidden or 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,27 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: discriminator_enum_single_value_anyof.yaml | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from enum import Enum | ||
| from typing import Literal | ||
|
|
||
| from pydantic import BaseModel, Field, RootModel | ||
|
|
||
|
|
||
| class ToolType(Enum): | ||
| function = 'function' | ||
|
|
||
|
|
||
| class FunctionToolCall(BaseModel): | ||
| id: str | None = None | ||
| type: Literal[ToolType.function] = ToolType.function | ||
|
|
||
|
|
||
| class CustomToolCall(BaseModel): | ||
| type: Literal['CustomToolCall'] = 'CustomToolCall' | ||
|
|
||
|
|
||
| class ToolCallUnion(RootModel[FunctionToolCall | CustomToolCall | None]): | ||
| root: FunctionToolCall | CustomToolCall | None = Field(None, discriminator='type') |
23 changes: 23 additions & 0 deletions
23
tests/data/expected/main/openapi/discriminator/force_optional_pydantic_v2.py
This file contains hidden or 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,23 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: discriminator_force_optional.yaml | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Literal | ||
|
|
||
| from pydantic import BaseModel, Field, RootModel | ||
|
|
||
|
|
||
| class CardPayment(BaseModel): | ||
| kind: Literal['card'] = 'card' | ||
| card_number: int | None = None | ||
|
|
||
|
|
||
| class CashPayment(BaseModel): | ||
| kind: Literal['cash'] = 'cash' | ||
| received_amount: int | None = None | ||
|
|
||
|
|
||
| class Payment(RootModel[CardPayment | CashPayment | None]): | ||
| root: CardPayment | CashPayment | None = Field(None, discriminator='kind') |
31 changes: 31 additions & 0 deletions
31
tests/data/expected/main/openapi/discriminator/force_optional_pydantic_v2_dataclass.py
This file contains hidden or 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,31 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: discriminator_force_optional.yaml | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Annotated, Literal | ||
|
|
||
| from pydantic import Field | ||
| from pydantic.dataclasses import dataclass | ||
| from typing_extensions import TypeAliasType | ||
|
|
||
|
|
||
| @dataclass | ||
| class CardPayment: | ||
| kind: Literal['card'] = 'card' | ||
| card_number: int | None = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class CashPayment: | ||
| kind: Literal['cash'] = 'cash' | ||
| received_amount: int | None = None | ||
|
|
||
|
|
||
| Payment = TypeAliasType( | ||
| "Payment", | ||
| Annotated[ | ||
| CardPayment | CashPayment | None, Field(None, discriminator='kind') | ||
| ], | ||
| ) |
31 changes: 31 additions & 0 deletions
31
tests/data/expected/main/openapi/discriminator/integer_mapping_use_enum_force_optional.py
This file contains hidden or 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,31 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: discriminator_integer_mapping.yaml | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from enum import IntEnum | ||
| from typing import Literal | ||
|
|
||
| from pydantic import BaseModel, Field, RootModel | ||
|
|
||
|
|
||
| class Kind(IntEnum): | ||
| integer_1 = 1 | ||
|
|
||
|
|
||
| class Foo(BaseModel): | ||
| kind: Literal[Kind.integer_1] = Kind.integer_1 | ||
|
|
||
|
|
||
| class Kind1(IntEnum): | ||
| integer_2 = 2 | ||
| integer_3 = 3 | ||
|
|
||
|
|
||
| class Bar(BaseModel): | ||
| kind: Literal[Kind1.integer_2, Kind1.integer_3] | ||
|
|
||
|
|
||
| class Base(RootModel[Foo | Bar | None]): | ||
| root: Foo | Bar | None = Field(None, discriminator='kind') |
23 changes: 23 additions & 0 deletions
23
tests/data/expected/main/openapi/discriminator/required_pydantic_v2.py
This file contains hidden or 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,23 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: discriminator_force_optional.yaml | ||
| # timestamp: 2019-07-26T00:00:00+00:00 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Literal | ||
|
|
||
| from pydantic import BaseModel, Field, RootModel | ||
|
|
||
|
|
||
| class CardPayment(BaseModel): | ||
| kind: Literal['card'] | ||
| card_number: int | ||
|
|
||
|
|
||
| class CashPayment(BaseModel): | ||
| kind: Literal['cash'] | ||
| received_amount: int | ||
|
|
||
|
|
||
| class Payment(RootModel[CardPayment | CashPayment]): | ||
| root: CardPayment | CashPayment = Field(..., discriminator='kind') |
This file contains hidden or 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 @@ | ||
| openapi: 3.1.0 | ||
| info: | ||
| title: Force Optional Discriminator | ||
| version: "0.1.0" | ||
| paths: {} | ||
| components: | ||
| schemas: | ||
| CardPayment: | ||
| type: object | ||
| required: | ||
| - kind | ||
| - card_number | ||
| properties: | ||
| kind: | ||
| type: string | ||
| const: card | ||
| card_number: | ||
| type: integer | ||
| CashPayment: | ||
| type: object | ||
| required: | ||
| - kind | ||
| - received_amount | ||
| properties: | ||
| kind: | ||
| type: string | ||
| received_amount: | ||
| type: integer | ||
| Payment: | ||
| oneOf: | ||
| - $ref: "#/components/schemas/CardPayment" | ||
| - $ref: "#/components/schemas/CashPayment" | ||
| discriminator: | ||
| propertyName: kind | ||
| mapping: | ||
| card: "#/components/schemas/CardPayment" | ||
| cash: "#/components/schemas/CashPayment" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.