Skip to content

Commit fe03b7e

Browse files
committed
feat: support AwareDatetime for timezone supported apis
1 parent a812525 commit fe03b7e

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

docs/references/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ Options:
2828
faster processing and better type support.
2929
Defaults to False.
3030

31+
--use-awaredatetime Use timezone-aware datetime objects instead of naive
32+
datetime objects. This ensures proper handling of
33+
timezone information in the generated models.
34+
Only supported with Pydantic v2.
35+
Defaults to False.
36+
3137
--custom-template-path TEXT
3238
Custom template path to use. Allows overriding of the
3339
built in templates.

src/openapi_python_generator/__main__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
help="Use the orjson library to serialize the data. This is faster than the default json library and provides "
3232
"serialization of datetimes and other types that are not supported by the default json library.",
3333
)
34+
@click.option(
35+
"--use-awaredatetime",
36+
is_flag=True,
37+
show_default=True,
38+
default=False,
39+
help="Use timezone-aware datetime objects instead of naive datetime objects. This ensures proper handling of "
40+
"timezone information in the generated models.",
41+
)
3442
@click.option(
3543
"--custom-template-path",
3644
type=str,
@@ -58,6 +66,7 @@ def main(
5866
library: Optional[HTTPLibrary] = HTTPLibrary.httpx,
5967
env_token_name: Optional[str] = None,
6068
use_orjson: bool = False,
69+
use_awaredatetime: bool = False,
6170
custom_template_path: Optional[str] = None,
6271
pydantic_version: PydanticVersion = PydanticVersion.V2,
6372
formatter: Formatter = Formatter.BLACK,
@@ -69,7 +78,7 @@ def main(
6978
an OUTPUT path, where the resulting client is created.
7079
"""
7180
generate_data(
72-
source, output, library, env_token_name, use_orjson, custom_template_path, pydantic_version, formatter
81+
source, output, library, env_token_name, use_orjson, use_awaredatetime, custom_template_path, pydantic_version, formatter
7382
)
7483

7584

src/openapi_python_generator/generate_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def generate_data(
180180
library: Optional[HTTPLibrary] = HTTPLibrary.httpx,
181181
env_token_name: Optional[str] = None,
182182
use_orjson: bool = False,
183+
use_awaredatetime: bool = False,
183184
custom_template_path: Optional[str] = None,
184185
pydantic_version: PydanticVersion = PydanticVersion.V2,
185186
formatter: Formatter = Formatter.BLACK,
@@ -195,6 +196,7 @@ def generate_data(
195196
library_config_dict[library],
196197
env_token_name,
197198
use_orjson,
199+
use_awaredatetime,
198200
custom_template_path,
199201
pydantic_version,
200202
)

src/openapi_python_generator/language_converters/python/common.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
_use_orjson: bool = False
88
_pydantic_version: PydanticVersion = PydanticVersion.V2
99
_custom_template_path: str = None
10+
_pydantic_use_awaredatetime: bool = False
1011
_symbol_ascii_strip_re = re.compile(r"[^A-Za-z0-9_]")
1112

1213

@@ -60,6 +61,23 @@ def get_custom_template_path() -> Optional[str]:
6061
return _custom_template_path
6162

6263

64+
def set_pydantic_use_awaredatetime(value: bool) -> None:
65+
"""
66+
Set whether to use AwareDateTime from pydantic instead of datetime.
67+
:param value: value of the variable
68+
"""
69+
global _pydantic_use_awaredatetime
70+
_pydantic_use_awaredatetime = value
71+
72+
def get_pydantic_use_awaredatetime() -> bool:
73+
"""
74+
Get whether to use AwareDateTime from pydantic instead of datetime.
75+
:return: value of the variable
76+
"""
77+
global _pydantic_use_awaredatetime
78+
return _pydantic_use_awaredatetime
79+
80+
6381
def normalize_symbol(symbol: str) -> str:
6482
"""
6583
Remove invalid characters & keywords in Python symbol names

src/openapi_python_generator/language_converters/python/generator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ def generator(
2222
library_config: LibraryConfig,
2323
env_token_name: Optional[str] = None,
2424
use_orjson: bool = False,
25+
use_awaredatetime: bool = False,
2526
custom_template_path: Optional[str] = None,
2627
pydantic_version: PydanticVersion = PydanticVersion.V2,
2728
) -> ConversionResult:
2829
"""
2930
Generate Python code from an OpenAPI 3.0 specification.
3031
"""
32+
if use_awaredatetime and pydantic_version != PydanticVersion.V2:
33+
raise ValueError("Timezone-aware datetime is only supported with Pydantic v2. Please use --pydantic-version v2.")
3134

3235
common.set_use_orjson(use_orjson)
3336
common.set_custom_template_path(custom_template_path)
3437
common.set_pydantic_version(pydantic_version)
38+
common.set_pydantic_use_awaredatetime(use_awaredatetime)
3539

3640
if data.components is not None:
3741
models = generate_models(data.components, pydantic_version)

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,12 @@ def type_converter( # noqa: C901
141141
# orjson and pydantic v2 both support datetime
142142
and (common.get_use_orjson() or common.get_pydantic_version() == PydanticVersion.V2)
143143
):
144-
converted_type = pre_type + "datetime" + post_type
145-
import_types = ["from datetime import datetime"]
144+
if common.get_pydantic_use_awaredatetime():
145+
converted_type = pre_type + "AwareDatetime" + post_type
146+
import_types = ["from pydantic import AwareDatetime"]
147+
else:
148+
converted_type = pre_type + "datetime" + post_type
149+
import_types = ["from datetime import datetime"]
146150
elif (
147151
schema.type == "string"
148152
and schema.schema_format == "date"

0 commit comments

Comments
 (0)