@@ -20,7 +20,7 @@ def load(filename):
20
20
return JsonRef .replace_refs (yaml .load (fp , Loader = CSafeLoader ))
21
21
22
22
23
- def type_to_python_helper (type_ , schema , alternative_name = None , in_list = False , typing = False ):
23
+ def basic_type_to_python (type_ , schema , typing = False ):
24
24
if type_ is None :
25
25
if typing :
26
26
return "Any"
@@ -39,11 +39,11 @@ def type_to_python_helper(type_, schema, alternative_name=None, in_list=False, t
39
39
elif type_ == "boolean" :
40
40
return "bool"
41
41
elif type_ == "array" :
42
- subtype = type_to_python (schema ["items" ], alternative_name = alternative_name + "Item" if alternative_name else None , in_list = True , typing = typing )
43
- if schema ["items" ].get ("nullable" ) and not typing :
44
- subtype += ", none_type"
42
+ subtype = type_to_python (schema ["items" ], typing = typing )
45
43
if typing :
46
44
return "List[{}]" .format (subtype )
45
+ if schema ["items" ].get ("nullable" ):
46
+ subtype += ", none_type"
47
47
return "[{}]" .format (subtype )
48
48
elif type_ == "object" :
49
49
if "additionalProperties" in schema :
@@ -57,70 +57,49 @@ def type_to_python_helper(type_, schema, alternative_name=None, in_list=False, t
57
57
if typing :
58
58
return f"Dict[str, { nested_name } ]"
59
59
return "{{str: ({},)}}" .format (nested_name )
60
- return (
61
- alternative_name
62
- if alternative_name
63
- and ("properties" in schema or "oneOf" in schema )
64
- else "dict"
65
- )
66
- elif type_ == "null" :
67
- return "none_type"
60
+ return "dict"
68
61
else :
69
62
raise ValueError (f"Unknown type { type_ } " )
70
63
71
64
72
- def type_to_python (schema , alternative_name = None , in_list = False , typing = False ):
65
+ def type_to_python (schema , typing = False ):
73
66
"""Return Python type name for the type."""
74
67
75
68
name = formatter .get_name (schema )
76
- # TODO: double check
77
- if name and "items" not in schema or formatter . is_list_model_whitelisted ( name ) :
78
- if "enum" in schema :
79
- return name
80
- if schema . get ( "type" , "object" ) == "object" or formatter . is_list_model_whitelisted ( name ):
81
- if typing and "oneOf" in schema :
82
- types = [ name ]
83
- types . extend ( get_oneof_types ( schema , typing = typing ))
84
- return f"Union[ { ',' . join ( types ) } ]"
69
+
70
+ if "oneOf" in schema :
71
+ types = list ( get_oneof_types ( schema , typing = typing ))
72
+ if name and typing :
73
+ types . insert ( 0 , name )
74
+ type_ = ", " . join ( types )
75
+ if typing :
76
+ return f"Union[ { type_ } ]"
77
+ elif name :
85
78
return name
79
+ return type_
86
80
87
- if name :
88
- alternative_name = name
81
+ if "enum" in schema :
82
+ return name
89
83
90
84
type_ = schema .get ("type" )
91
- if type_ is None :
92
- if "oneOf" in schema and in_list :
93
- type_ = ""
94
- for child in schema ["oneOf" ]:
95
- # We do not generate model for nested primitive oneOfs
96
- if in_list and "items" in child and child ["items" ].get ("type" ) in PRIMITIVE_TYPES :
97
- type_ += f"{ type_to_python_helper (child .get ('type' ), child , in_list = in_list , typing = typing )} ,"
98
- else :
99
- type_ += f"{ type_to_python (child , in_list = in_list , typing = typing )} ,"
100
- if typing :
101
- return f"Union[{ type_ } ]"
102
- return type_
103
- if "items" in schema :
104
- type_ = "array"
105
85
106
- return type_to_python_helper (type_ , schema , alternative_name = alternative_name , in_list = in_list , typing = typing )
86
+ if name and (type_ == "object" or formatter .is_list_model_whitelisted (name )):
87
+ return name
88
+
89
+ return basic_type_to_python (type_ , schema , typing = typing )
107
90
108
91
109
92
def get_type_for_attribute (schema , attribute , current_name = None ):
110
93
"""Return Python type name for the attribute."""
111
94
child_schema = schema .get ("properties" , {}).get (attribute )
112
- alternative_name = current_name + formatter .camel_case (attribute ) if current_name else None
113
- return type_to_python (child_schema , alternative_name = alternative_name )
95
+ return type_to_python (child_schema )
114
96
115
97
116
98
def get_typing_for_attribute (schema , attribute , current_name = None , optional = False ):
117
99
child_schema = schema .get ("properties" , {}).get (attribute )
118
- alternative_name = current_name + formatter .camel_case (attribute ) if current_name else None
119
- attr_type = type_to_python (child_schema , alternative_name = alternative_name , typing = True )
100
+ attr_type = type_to_python (child_schema , typing = True )
120
101
if child_schema .get ("nullable" ):
121
- if optional :
122
- return f"Union[{ attr_type } , none_type, UnsetType]"
123
- return f"Union[{ attr_type } , none_type]"
102
+ attr_type = f"Union[{ attr_type } , none_type]"
124
103
if optional :
125
104
if attr_type .startswith ("Union" ):
126
105
return attr_type [:- 1 ] + ", UnsetType]"
@@ -137,8 +116,7 @@ def get_types_for_attribute(schema, attribute, current_name=None):
137
116
138
117
139
118
def get_type_for_items (schema ):
140
- name = formatter .get_name (schema .get ("items" ))
141
- return name
119
+ return formatter .get_name (schema .get ("items" ))
142
120
143
121
144
122
def get_type_for_parameter (parameter , typing = False ):
@@ -158,44 +136,38 @@ def get_enum_type(schema):
158
136
elif type_ == "string" :
159
137
return "str"
160
138
139
+ raise ValueError (f"Unknown type { type_ } " )
140
+
161
141
162
142
def get_enum_default (model ):
163
143
return model ["enum" ][0 ] if len (model ["enum" ]) == 1 else model .get ("default" )
164
144
165
145
166
- def child_models (schema , alternative_name = None , seen = None , in_list = False ):
146
+ def child_models (schema , alternative_name = None , seen = None ):
167
147
seen = seen or set ()
168
148
current_name = formatter .get_name (schema )
169
149
name = current_name or alternative_name
170
150
151
+ if name in seen :
152
+ return
153
+
171
154
has_sub_models = False
172
155
if "oneOf" in schema :
173
- has_sub_models = not in_list and not formatter .is_list_model_whitelisted (name )
156
+ has_sub_models = not formatter .is_list_model_whitelisted (name )
174
157
for child in schema ["oneOf" ]:
175
158
sub_models = list (child_models (child , seen = seen ))
176
159
if sub_models :
177
160
has_sub_models = True
178
161
yield from sub_models
179
- if in_list and not has_sub_models :
162
+ if not has_sub_models :
180
163
return
181
164
182
165
if "items" in schema :
183
- if formatter .is_list_model_whitelisted (name ):
184
- alt_name = None
185
- else :
186
- alt_name = name + "Item" if name is not None else None
187
-
188
- yield from child_models (schema ["items" ], alternative_name = alt_name , seen = seen , in_list = True )
166
+ yield from child_models (schema ["items" ], seen = seen )
189
167
190
168
if schema .get ("type" ) == "object" or "properties" in schema or has_sub_models :
191
- if not has_sub_models and name is None :
192
- # this is a basic map object so we don't need a type
193
- return
194
-
195
169
if name is None :
196
- return
197
-
198
- if name in seen :
170
+ # this is a basic map object so we don't need a type
199
171
return
200
172
201
173
if "properties" in schema or has_sub_models :
@@ -210,9 +182,6 @@ def child_models(schema, alternative_name=None, seen=None, in_list=False):
210
182
yield from child_models (child , alternative_name = name + formatter .camel_case (key ), seen = seen )
211
183
212
184
if current_name and schema .get ("type" ) == "array" :
213
- if name in seen :
214
- return
215
-
216
185
if formatter .is_list_model_whitelisted (name ):
217
186
seen .add (name )
218
187
yield name , schema
@@ -221,9 +190,6 @@ def child_models(schema, alternative_name=None, seen=None, in_list=False):
221
190
if name is None :
222
191
raise ValueError (f"Schema { schema } has no name" )
223
192
224
- if name in seen :
225
- return
226
-
227
193
seen .add (name )
228
194
yield name , schema
229
195
@@ -266,9 +232,7 @@ def find_non_primitive_type(schema):
266
232
sub_type = schema .get ("type" )
267
233
if sub_type == "array" :
268
234
return find_non_primitive_type (schema ["items" ])
269
- if sub_type not in PRIMITIVE_TYPES :
270
- return True
271
- return False
235
+ return sub_type not in PRIMITIVE_TYPES
272
236
273
237
274
238
def get_references_for_model (model , model_name ):
@@ -290,13 +254,13 @@ def get_references_for_model(model, model_name):
290
254
if name and formatter .is_list_model_whitelisted (name ):
291
255
result [name ] = None
292
256
else :
293
- name = formatter .get_name (definition .get ("items" ))
294
- if name and find_non_primitive_type ( definition [ "items" ]):
295
- result [ name ] = None
296
- elif name and formatter . is_list_model_whitelisted ( name ):
297
- result [name ] = None
298
- elif formatter . get_name ( definition ) and definition ["items" ].get ("type" ) not in PRIMITIVE_TYPES :
299
- result [formatter . get_name ( definition ) + " Item" ] = None
257
+ items_name = formatter .get_name (definition .get ("items" ))
258
+ if items_name and (
259
+ find_non_primitive_type ( definition [ "items" ]) or formatter . is_list_model_whitelisted ( items_name )
260
+ ):
261
+ result [items_name ] = None
262
+ elif name and definition ["items" ].get ("type" ) not in PRIMITIVE_TYPES :
263
+ result [f" { name } Item" ] = None
300
264
301
265
elif definition .get ("properties" ) and top_name :
302
266
result [top_name + formatter .camel_case (key )] = None
@@ -362,23 +326,8 @@ def get_oneof_types(model, typing=False):
362
326
name = formatter .get_name (schema )
363
327
if type_ == "object" or formatter .is_list_model_whitelisted (name ):
364
328
yield name
365
- elif type_ == "array" :
366
- name = formatter .get_name (schema ["items" ])
367
- if name :
368
- if typing :
369
- yield f"List[{ name } ]"
370
- else :
371
- yield f"[{ name } ]"
372
- elif type_ == "integer" :
373
- yield "int"
374
- elif type_ == "string" :
375
- yield "str"
376
- elif type_ == "number" :
377
- yield "float"
378
- elif type_ == "boolean" :
379
- yield "bool"
380
329
else :
381
- raise NotImplementedError ( f"Type { type_ } not implemented" )
330
+ yield basic_type_to_python ( type_ , schema , typing = typing )
382
331
383
332
384
333
def get_oneof_models (model ):
0 commit comments