Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ def type_converter(schema: Schema, required: bool = False, model_name: Optional
schema.schema_format is None or not common.get_use_orjson()
):
converted_type = pre_type + "str" + post_type
elif schema.type == "string" and schema.schema_format.startswith("uuid") and common.get_use_orjson():
if len(schema.schema_format) > 4 and schema.schema_format[4].isnumeric():
uuid_type = schema.schema_format.upper()
converted_type = pre_type + uuid_type + post_type
import_types = ["from pydantic import " + uuid_type]
else:
converted_type = pre_type + "UUID" + post_type
import_types = ["from uuid import UUID"]
elif schema.type == "string" and schema.schema_format == "date-time":
converted_type = pre_type + "datetime" + post_type
import_types = ["from datetime import datetime"]
Expand Down
43 changes: 43 additions & 0 deletions tests/test_data/test_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,49 @@
}
}
},
"UserWithUuid": {
"title": "User",
"required": [
"id",
"username",
"email",
"password"
],
"type": "object",
"properties": {
"id": {
"title": "Id",
"type": "string",
"format": "uuid"
},
"username": {
"title": "Username",
"type": "string"
},
"email": {
"title": "Email",
"type": "string"
},
"password": {
"title": "Password",
"type": "string"
},
"is_active": {
"title": "Is Active",
"type": "boolean"
},
"created_at": {
"title": "Created At",
"type": "string",
"format": "date-time"
},
"some_foreign_key": {
"title": "Some Foreign Key",
"type": "string",
"format": "uuid4"
}
}
},
"EnumComponent": {
"title": "EnumComponent",
"enum": [
Expand Down
56 changes: 56 additions & 0 deletions tests/test_model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@
Schema(type="null"),
TypeConversion(original_type="null", converted_type="Any"),
),
(
Schema(type="string", schema_format="uuid"),
TypeConversion(original_type="string", converted_type="str"),
),
(
Schema(type="string", schema_format="uuid1"),
TypeConversion(original_type="string", converted_type="str"),
),
(
Schema(type="string", schema_format="uuid3"),
TypeConversion(original_type="string", converted_type="str"),
),
(
Schema(type="string", schema_format="uuid4"),
TypeConversion(original_type="string", converted_type="str"),
),
(
Schema(type="string", schema_format="uuid5"),
TypeConversion(original_type="string", converted_type="str"),
),
],
)
def test_type_converter_simple(test_openapi_types, expected_python_types):
Expand Down Expand Up @@ -138,6 +158,42 @@ def test_type_converter_simple(test_openapi_types, expected_python_types):
Schema(type="null"),
TypeConversion(original_type="null", converted_type="Any"),
),
(
Schema(type="string", schema_format="uuid"),
TypeConversion(original_type="string", converted_type="UUID", import_types=['from uuid import UUID']),
),
(
Schema(type="string", schema_format="uuid1"),
TypeConversion(
original_type="string",
converted_type="UUID1",
import_types=['from pydantic import UUID1']
),
),
(
Schema(type="string", schema_format="uuid3"),
TypeConversion(
original_type="string",
converted_type="UUID3",
import_types=['from pydantic import UUID3']
),
),
(
Schema(type="string", schema_format="uuid4"),
TypeConversion(
original_type="string",
converted_type="UUID4",
import_types=['from pydantic import UUID4']
),
),
(
Schema(type="string", schema_format="uuid5"),
TypeConversion(
original_type="string",
converted_type="UUID5",
import_types=['from pydantic import UUID5']
),
)
],
)
def test_type_converter_simple_orjson(test_openapi_types, expected_python_types):
Expand Down