Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,11 @@ def type_converter( # noqa: C901
*[i.import_types for i in conversions if i.import_types is not None]
)
)
# We only want to auto convert to datetime if orjson is used throghout the code, otherwise we can not
# serialize it to JSON.
elif schema.type == "string" and (
schema.schema_format is None or not common.get_use_orjson()
):
converted_type = pre_type + "str" + post_type
# With custom string format fields, in order to cast these to strict types (e.g. date, datetime, UUID)
# orjson is required for JSON serialiation.
elif (
schema.type == "string"
and schema.schema_format is not None
and schema.schema_format.startswith("uuid")
and common.get_use_orjson()
):
Expand All @@ -136,9 +133,17 @@ def type_converter( # noqa: C901
else:
converted_type = pre_type + "UUID" + post_type
import_types = ["from uuid import UUID"]
elif schema.type == "string" and schema.schema_format == "date-time":
elif schema.type == "string" and schema.schema_format == "date-time" and common.get_use_orjson():
converted_type = pre_type + "datetime" + post_type
import_types = ["from datetime import datetime"]
elif schema.type == "string" and schema.schema_format == "date" and common.get_use_orjson():
converted_type = pre_type + "date" + post_type
import_types = ["from datetime import date"]
elif schema.type == "string" and schema.schema_format == "decimal" and common.get_use_orjson():
converted_type = pre_type + "Decimal" + post_type
import_types = ["from decimal import Decimal"]
elif schema.type == "string":
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This also captures unknown "schema_format" that would earlier result in "unknown type string" errors.

converted_type = pre_type + "str" + post_type
elif schema.type == "integer":
converted_type = pre_type + "int" + post_type
elif schema.type == "number":
Expand Down
10 changes: 10 additions & 0 deletions tests/test_data/test_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,16 @@
"title": "Created At",
"type": "string",
"format": "date-time"
},
"birthdate": {
"title": "Birthdate",
"type": "string",
"format": "date"
},
"position": {
"title": "Position",
"type": "string",
"format": "decimal"
}
}
},
Expand Down
31 changes: 31 additions & 0 deletions tests/test_model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
Schema(type=DataType.STRING, schema_format="date-time"),
TypeConversion(original_type="string", converted_type="str"),
),
(
Schema(type=DataType.STRING, schema_format="date"),
TypeConversion(original_type="string", converted_type="str"),
),
(
Schema(type=DataType.STRING, schema_format="decimal"),
TypeConversion(original_type="string", converted_type="str"),
),
(
Schema(type=DataType.OBJECT),
TypeConversion(original_type="object", converted_type="Dict[str, Any]"),
Expand Down Expand Up @@ -137,6 +145,29 @@ def test_type_converter_simple(test_openapi_types, expected_python_types):
import_types=["from datetime import datetime"],
),
),
(
Schema(type=DataType.STRING, schema_format="date"),
TypeConversion(
original_type="string",
converted_type="date",
import_types=["from datetime import date"],
),
),
(
Schema(type=DataType.STRING, schema_format="decimal"),
TypeConversion(
original_type="string",
converted_type="Decimal",
import_types=["from decimal import Decimal"],
),
),
(
Schema(type=DataType.STRING, schema_format="email"),
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Note that "email" isn't specifically handled by the generator, this is just the test to make sure unexpected formats don't break.

TypeConversion(
original_type="string",
converted_type="str",
),
),
(
Schema(type=DataType.OBJECT),
TypeConversion(original_type="object", converted_type="Dict[str, Any]"),
Expand Down