4
4
This module provides utility functions for working with OpenAPI schemas.
5
5
"""
6
6
7
- from typing import Any , Dict , List , Optional , Union
8
- from datetime import date , datetime
9
- from decimal import Decimal
10
- from uuid import UUID
11
-
12
- PYTHON_TYPE_IMPORTS = {
13
- "List" : List ,
14
- "Dict" : Dict ,
15
- "Any" : Any ,
16
- "Optional" : Optional ,
17
- "Union" : Union ,
18
- "date" : date ,
19
- "datetime" : datetime ,
20
- "Decimal" : Decimal ,
21
- "UUID" : UUID ,
22
- }
23
- # Type mapping from OpenAPI types to Python types
24
- OPENAPI_PYTHON_TYPES_MAP = {
25
- # Core data types (OpenAPI 3.x)
26
- "string" : "str" ,
27
- "number" : "Union[float, Decimal]" , # Could be float or Decimal for precision
28
- "integer" : "int" ,
29
- "boolean" : "bool" ,
30
- "null" : "None" ,
31
- # Complex types
32
- "object" : "Dict[str, Any]" , # More specific than Dict[Any, Any]
33
- "array" : "List[Any]" ,
34
- # Numeric formats
35
- "int32" : "int" ,
36
- "int64" : "int" ,
37
- "float" : "float" ,
38
- "double" : "float" ,
39
- "decimal" : "Decimal" ,
40
- # String formats - Common
41
- "date" : "date" , # datetime.date
42
- "date-time" : "datetime" , # datetime.datetime
43
- "time" : "str" , # Could use datetime.time
44
- "duration" : "str" , # Could use datetime.timedelta
45
- "password" : "str" ,
46
- "byte" : "bytes" , # base64 encoded
47
- "binary" : "bytes" , # raw binary
48
- # String formats - Extended
49
- "email" : "str" ,
50
- "uuid" : "UUID" , # uuid.UUID
51
- "uri" : "str" ,
52
- "uri-reference" : "str" ,
53
- "uri-template" : "str" ,
54
- "url" : "str" ,
55
- "hostname" : "str" ,
56
- "ipv4" : "str" ,
57
- "ipv6" : "str" ,
58
- "regex" : "str" ,
59
- "json-pointer" : "str" ,
60
- "relative-json-pointer" : "str" ,
61
- # Rich text formats
62
- "markdown" : "str" ,
63
- "html" : "str" ,
64
- # Media types
65
- "image/*" : "bytes" ,
66
- "audio/*" : "bytes" ,
67
- "video/*" : "bytes" ,
68
- "application/*" : "bytes" ,
69
- # Special formats
70
- "format" : "str" , # Custom format string
71
- "pattern" : "str" , # Regular expression pattern
72
- "contentEncoding" : "str" , # e.g., base64, quoted-printable
73
- "contentMediaType" : "str" , # MIME type
74
- # Additional numeric formats
75
- "currency" : "Decimal" , # For precise decimal calculations
76
- "percentage" : "float" ,
77
- # Geographic coordinates
78
- "latitude" : "float" ,
79
- "longitude" : "float" ,
80
- # Time-related
81
- "timezone" : "str" , # Could use zoneinfo.ZoneInfo in Python 3.9+
82
- "unix-time" : "int" , # Unix timestamp
83
- "iso-week-date" : "str" , # ISO 8601 week date
84
- # Specialized string formats
85
- "isbn" : "str" ,
86
- "issn" : "str" ,
87
- "iban" : "str" ,
88
- "credit-card" : "str" ,
89
- "phone" : "str" ,
90
- "postal-code" : "str" ,
91
- "language-code" : "str" , # ISO 639 language codes
92
- "country-code" : "str" , # ISO 3166 country codes
93
- "currency-code" : "str" , # ISO 4217 currency codes
94
- # Default fallback
95
- "unknown" : "Any" ,
96
- }
7
+ from typing import Any , Dict , List , Optional
97
8
98
9
99
10
def get_single_param_type_from_schema (param_schema : Dict [str , Any ]) -> str :
@@ -111,21 +22,6 @@ def get_single_param_type_from_schema(param_schema: Dict[str, Any]) -> str:
111
22
return param_schema .get ("type" , "string" )
112
23
113
24
114
- def get_python_type_and_default (parsed_param_schema : Dict [str , Any ]) -> tuple [str , bool ]:
115
- """
116
- Parse parameters into a python type and default value string.
117
- Returns:
118
- A tuple containing:
119
- - A string representing the Python type annotation (e.g. "str", "int = 0", etc.)
120
- - A boolean indicating whether a default value is present
121
- """
122
- # Handle direct type specification
123
- python_type = OPENAPI_PYTHON_TYPES_MAP .get (parsed_param_schema .get ("type" , "" ), "Any" )
124
- if "default" in parsed_param_schema :
125
- return f"{ python_type } = { parsed_param_schema .get ('default' )} " , True
126
- return python_type , False
127
-
128
-
129
25
def resolve_schema_references (schema_part : Dict [str , Any ], reference_schema : Dict [str , Any ]) -> Dict [str , Any ]:
130
26
"""
131
27
Resolve schema references in OpenAPI schemas.
0 commit comments