@@ -34,24 +34,18 @@ def download_schemafile(
34
34
return schemapath
35
35
36
36
def generate_class (class_name : str , class_schema : Dict [str , Any ]) -> str :
37
-
38
- # Define a list of primitive types
39
- # primitive_types = ['string', 'number', 'integer', 'boolean']
40
37
class_name = get_valid_identifier (class_name )
41
38
42
39
# Check if the schema defines a simple type (like string, number) without properties
43
- if 'type' in class_schema and 'properties' not in class_schema : #DISCUSS: Change this to not isinstance(class_schema, Iterable)
44
- #assert not isinstance(class_schema, Iterable)
40
+ if 'type' in class_schema and 'properties' not in class_schema :
45
41
return f"class { class_name } :\n def __init__(self):\n pass\n "
46
42
47
43
# Check for '$ref' and handle it
48
44
if '$ref' in class_schema :
49
45
ref_class_name = class_schema ['$ref' ].split ('/' )[- 1 ]
50
46
return f"\n class { class_name } :\n pass # This is a reference to '{ ref_class_name } '\n "
51
-
52
-
53
47
if 'anyOf' in class_schema :
54
- return generate_any_of_class (class_name , class_schema ['anyOf' ])
48
+ return generate_any_of_class (class_name , class_schema ['anyOf' ])
55
49
56
50
# Extract properties and required fields
57
51
properties = class_schema .get ('properties' , {})
@@ -64,32 +58,32 @@ def generate_class(class_name: str, class_schema: Dict[str, Any]) -> str:
64
58
optional_params = []
65
59
66
60
# Ensuring all the property names are valid Python identifiers
67
- property_items = list ( properties . items ())
68
- for prop , prop_schema in property_items :
61
+ valid_properties = {}
62
+ for prop , prop_schema in properties . items () :
69
63
valid_prop = get_valid_identifier (prop )
70
- if valid_prop != prop :
71
- properties .pop (prop )
72
- properties [valid_prop ] = prop_schema
64
+ valid_properties [valid_prop ] = prop_schema
73
65
74
- for prop , prop_schema in properties .items ():
75
- type_hint = get_type_hint (prop_schema )
66
+ for prop , prop_schema in valid_properties .items ():
67
+ if 'anyOf' in prop_schema :
68
+ # Handle anyOf case
69
+ type_hint = f"Union[{ ', ' .join (get_type_hint (item ) for item in prop_schema ['anyOf' ])} ]"
70
+ else :
71
+ type_hint = get_type_hint (prop_schema )
76
72
77
73
if prop in required :
78
74
# Required parameters should not have default values
79
75
class_def += f", { prop } : { type_hint } "
80
76
else :
81
77
# Ensure we add optional parameters last
82
- optional_params .append ((prop , prop_schema ))
78
+ optional_params .append ((prop , type_hint ))
83
79
84
- for prop , prop_schema in optional_params :
85
- # Optional parameters should have a default value of None
80
+ for prop , type_hint in optional_params :
86
81
class_def += f", { prop } : { type_hint } = None"
87
82
88
-
89
83
class_def += "):\n "
90
84
91
85
# Generate attribute assignments in __init__
92
- for prop in properties :
86
+ for prop in valid_properties :
93
87
class_def += f" self.{ prop } = { prop } \n "
94
88
95
89
return class_def
@@ -103,13 +97,12 @@ def get_type_union(types: List[str]):
103
97
if "Any" in unique_types :
104
98
unique_types .remove ("Any" )
105
99
unique_types .append ("Any" )
106
-
107
100
return f'Union[{ ", " .join (unique_types )} ]'
108
101
109
102
def generate_any_of_class (class_name : str , any_of_schemas : List [Dict [str , Any ]]) -> str :
110
103
types = [get_type_hint (schema ) for schema in any_of_schemas ]
111
104
type_union = get_type_union (types )
112
-
105
+
113
106
class_def = f"class { class_name } :\n "
114
107
class_def += f" def __init__(self, value: { type_union } ):\n "
115
108
class_def += " self.value = value\n "
@@ -126,9 +119,9 @@ def get_type_hint(type_schema: Dict[str, Any]) -> str:
126
119
127
120
datatype = get_type_hint (items_schema )
128
121
return f"List[{ datatype } ]"
129
-
122
+
130
123
if 'type' in type_schema :
131
- if isinstance (type_schema ['type' ], Iterable ):
124
+ if isinstance (type_schema ['type' ], list ):
132
125
types = []
133
126
for t in type_schema ['type' ]:
134
127
datatype = KNOWN_PRIMITIVES .get (t )
@@ -144,7 +137,6 @@ def get_type_hint(type_schema: Dict[str, Any]) -> str:
144
137
return 'Any'
145
138
return datatype
146
139
elif 'anyOf' in type_schema :
147
- #print(f"anyOf to iterate: {type_schema}")
148
140
types = [get_type_hint (option ) for option in type_schema ['anyOf' ]]
149
141
return get_type_union (types )
150
142
elif '$ref' in type_schema :
@@ -168,7 +160,6 @@ def generate_schema_wrapper(schema_file: Path, output_file: Path) -> str:
168
160
# output_file.parent.mkdir(parents=True, exist_ok=True)
169
161
170
162
for name , schema in rootschema_definitions .items ():
171
- #print(name)
172
163
dependencies = get_dependencies (schema )
173
164
if dependencies :
174
165
ts .add (name , * dependencies )
@@ -186,7 +177,6 @@ def generate_schema_wrapper(schema_file: Path, output_file: Path) -> str:
186
177
187
178
generated_classes = "\n \n " .join (definitions .values ())
188
179
generated_classes = "from typing import List, Dict, Any, Union\n \n " + generated_classes
189
- #print(generated_classes)
190
180
191
181
with open (output_file , 'w' ) as f :
192
182
f .write (generated_classes )
0 commit comments