Skip to content

Commit 5a8211e

Browse files
committed
feat: support additional string format types, preventing failure on unknown types
1 parent 9430d18 commit 5a8211e

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,11 @@ def type_converter( # noqa: C901
118118
*[i.import_types for i in conversions if i.import_types is not None]
119119
)
120120
)
121-
# We only want to auto convert to datetime if orjson is used throghout the code, otherwise we can not
122-
# serialize it to JSON.
123-
elif schema.type == "string" and (
124-
schema.schema_format is None or not common.get_use_orjson()
125-
):
126-
converted_type = pre_type + "str" + post_type
121+
# With custom string format fields, in order to cast these to strict types (e.g. date, datetime, UUID)
122+
# orjson is required for JSON serialiation.
127123
elif (
128124
schema.type == "string"
125+
and schema.schema_format is not None
129126
and schema.schema_format.startswith("uuid")
130127
and common.get_use_orjson()
131128
):
@@ -136,9 +133,17 @@ def type_converter( # noqa: C901
136133
else:
137134
converted_type = pre_type + "UUID" + post_type
138135
import_types = ["from uuid import UUID"]
139-
elif schema.type == "string" and schema.schema_format == "date-time":
136+
elif schema.type == "string" and schema.schema_format == "date-time" and common.get_use_orjson():
140137
converted_type = pre_type + "datetime" + post_type
141138
import_types = ["from datetime import datetime"]
139+
elif schema.type == "string" and schema.schema_format == "date" and common.get_use_orjson():
140+
converted_type = pre_type + "date" + post_type
141+
import_types = ["from datetime import date"]
142+
elif schema.type == "string" and schema.schema_format == "decimal" and common.get_use_orjson():
143+
converted_type = pre_type + "Decimal" + post_type
144+
import_types = ["from decimal import Decimal"]
145+
elif schema.type == "string":
146+
converted_type = pre_type + "str" + post_type
142147
elif schema.type == "integer":
143148
converted_type = pre_type + "int" + post_type
144149
elif schema.type == "number":

tests/test_data/test_api.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@
527527
"title": "Created At",
528528
"type": "string",
529529
"format": "date-time"
530+
},
531+
"birthdate": {
532+
"title": "Birthdate",
533+
"type": "string",
534+
"format": "date"
535+
},
536+
"position": {
537+
"title": "Position",
538+
"type": "string",
539+
"format": "decimal"
530540
}
531541
}
532542
},

tests/test_model_generator.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
Schema(type=DataType.STRING, schema_format="date-time"),
4444
TypeConversion(original_type="string", converted_type="str"),
4545
),
46+
(
47+
Schema(type=DataType.STRING, schema_format="date"),
48+
TypeConversion(original_type="string", converted_type="str"),
49+
),
50+
(
51+
Schema(type=DataType.STRING, schema_format="decimal"),
52+
TypeConversion(original_type="string", converted_type="str"),
53+
),
4654
(
4755
Schema(type=DataType.OBJECT),
4856
TypeConversion(original_type="object", converted_type="Dict[str, Any]"),
@@ -137,6 +145,29 @@ def test_type_converter_simple(test_openapi_types, expected_python_types):
137145
import_types=["from datetime import datetime"],
138146
),
139147
),
148+
(
149+
Schema(type=DataType.STRING, schema_format="date"),
150+
TypeConversion(
151+
original_type="string",
152+
converted_type="date",
153+
import_types=["from datetime import date"],
154+
),
155+
),
156+
(
157+
Schema(type=DataType.STRING, schema_format="decimal"),
158+
TypeConversion(
159+
original_type="string",
160+
converted_type="Decimal",
161+
import_types=["from decimal import Decimal"],
162+
),
163+
),
164+
(
165+
Schema(type=DataType.STRING, schema_format="email"),
166+
TypeConversion(
167+
original_type="string",
168+
converted_type="str",
169+
),
170+
),
140171
(
141172
Schema(type=DataType.OBJECT),
142173
TypeConversion(original_type="object", converted_type="Dict[str, Any]"),

0 commit comments

Comments
 (0)