Skip to content

Commit 104206e

Browse files
Merge pull request #35 from alliefitter/support_uuid_format
Added support for UUID formatted string when using orjson.
2 parents a47d899 + 316f2f7 commit 104206e

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

src/openapi_python_generator/language_converters/python/model_generator.py

+8
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ def type_converter(schema: Schema, required: bool = False, model_name: Optional
119119
schema.schema_format is None or not common.get_use_orjson()
120120
):
121121
converted_type = pre_type + "str" + post_type
122+
elif schema.type == "string" and schema.schema_format.startswith("uuid") and common.get_use_orjson():
123+
if len(schema.schema_format) > 4 and schema.schema_format[4].isnumeric():
124+
uuid_type = schema.schema_format.upper()
125+
converted_type = pre_type + uuid_type + post_type
126+
import_types = ["from pydantic import " + uuid_type]
127+
else:
128+
converted_type = pre_type + "UUID" + post_type
129+
import_types = ["from uuid import UUID"]
122130
elif schema.type == "string" and schema.schema_format == "date-time":
123131
converted_type = pre_type + "datetime" + post_type
124132
import_types = ["from datetime import datetime"]

tests/test_data/test_api.json

+43
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,49 @@
530530
}
531531
}
532532
},
533+
"UserWithUuid": {
534+
"title": "User",
535+
"required": [
536+
"id",
537+
"username",
538+
"email",
539+
"password"
540+
],
541+
"type": "object",
542+
"properties": {
543+
"id": {
544+
"title": "Id",
545+
"type": "string",
546+
"format": "uuid"
547+
},
548+
"username": {
549+
"title": "Username",
550+
"type": "string"
551+
},
552+
"email": {
553+
"title": "Email",
554+
"type": "string"
555+
},
556+
"password": {
557+
"title": "Password",
558+
"type": "string"
559+
},
560+
"is_active": {
561+
"title": "Is Active",
562+
"type": "boolean"
563+
},
564+
"created_at": {
565+
"title": "Created At",
566+
"type": "string",
567+
"format": "date-time"
568+
},
569+
"some_foreign_key": {
570+
"title": "Some Foreign Key",
571+
"type": "string",
572+
"format": "uuid4"
573+
}
574+
}
575+
},
533576
"EnumComponent": {
534577
"title": "EnumComponent",
535578
"enum": [

tests/test_model_generator.py

+56
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@
6868
Schema(type="null"),
6969
TypeConversion(original_type="null", converted_type="Any"),
7070
),
71+
(
72+
Schema(type="string", schema_format="uuid"),
73+
TypeConversion(original_type="string", converted_type="str"),
74+
),
75+
(
76+
Schema(type="string", schema_format="uuid1"),
77+
TypeConversion(original_type="string", converted_type="str"),
78+
),
79+
(
80+
Schema(type="string", schema_format="uuid3"),
81+
TypeConversion(original_type="string", converted_type="str"),
82+
),
83+
(
84+
Schema(type="string", schema_format="uuid4"),
85+
TypeConversion(original_type="string", converted_type="str"),
86+
),
87+
(
88+
Schema(type="string", schema_format="uuid5"),
89+
TypeConversion(original_type="string", converted_type="str"),
90+
),
7191
],
7292
)
7393
def test_type_converter_simple(test_openapi_types, expected_python_types):
@@ -138,6 +158,42 @@ def test_type_converter_simple(test_openapi_types, expected_python_types):
138158
Schema(type="null"),
139159
TypeConversion(original_type="null", converted_type="Any"),
140160
),
161+
(
162+
Schema(type="string", schema_format="uuid"),
163+
TypeConversion(original_type="string", converted_type="UUID", import_types=['from uuid import UUID']),
164+
),
165+
(
166+
Schema(type="string", schema_format="uuid1"),
167+
TypeConversion(
168+
original_type="string",
169+
converted_type="UUID1",
170+
import_types=['from pydantic import UUID1']
171+
),
172+
),
173+
(
174+
Schema(type="string", schema_format="uuid3"),
175+
TypeConversion(
176+
original_type="string",
177+
converted_type="UUID3",
178+
import_types=['from pydantic import UUID3']
179+
),
180+
),
181+
(
182+
Schema(type="string", schema_format="uuid4"),
183+
TypeConversion(
184+
original_type="string",
185+
converted_type="UUID4",
186+
import_types=['from pydantic import UUID4']
187+
),
188+
),
189+
(
190+
Schema(type="string", schema_format="uuid5"),
191+
TypeConversion(
192+
original_type="string",
193+
converted_type="UUID5",
194+
import_types=['from pydantic import UUID5']
195+
),
196+
)
141197
],
142198
)
143199
def test_type_converter_simple_orjson(test_openapi_types, expected_python_types):

0 commit comments

Comments
 (0)