Skip to content
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

Change ordering of actions #1961

Merged
merged 9 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datamodel_code_generator/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,8 +1370,8 @@ class Processed(NamedTuple):
)
self.__set_default_enum_member(models)
self.__sort_models(models, imports)
self.__set_one_literal_on_default(models)
self.__apply_discriminator_type(models, imports)
self.__set_one_literal_on_default(models)

processed_models.append(
Processed(module, models, init, imports, scoped_model_resolver)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# generated by datamodel-codegen:
# filename: discriminator_enum.yaml
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from enum import Enum
from typing import Literal, Union

from pydantic import BaseModel, Field, RootModel


class RequestVersionEnum(Enum):
v1 = 'v1'
v2 = 'v2'


class RequestBase(BaseModel):
version: RequestVersionEnum


class RequestV1(RequestBase):
request_id: str = Field(..., description='there is description', title='test title')
version: Literal['v1'] = 'v1'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generator now correctly adds the default for the literal



class RequestV2(RequestBase):
version: Literal['v2'] = 'v2'


class Request(RootModel[Union[RequestV1, RequestV2]]):
root: Union[RequestV1, RequestV2] = Field(..., discriminator='version')
28 changes: 28 additions & 0 deletions tests/main/openapi/test_main_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2798,3 +2798,31 @@ def test_main_openapi_msgspec_use_annotated_with_field_constraints():
/ 'msgspec_use_annotated_with_field_constraints.py'
).read_text()
)


@freeze_time('2019-07-26')
def test_main_openapi_discriminator_one_literal_as_default():
with TemporaryDirectory() as output_dir:
output_file: Path = Path(output_dir) / 'output.py'
return_code: Exit = main(
[
'--input',
str(OPEN_API_DATA_PATH / 'discriminator_enum.yaml'),
'--output',
str(output_file),
'--input-file-type',
'openapi',
'--output-model-type',
'pydantic_v2.BaseModel',
'--use-one-literal-as-default',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting --use-one-literal-as-default in the test

]
)
assert return_code == Exit.OK
assert (
output_file.read_text()
== (
EXPECTED_OPENAPI_PATH
/ 'discriminator'
/ 'enum_one_literal_as_default.py'
).read_text()
)
Loading