@@ -81,6 +81,7 @@ def generate_operation_id(*, path: str, method: str) -> str:
81
81
return f"{ method } _{ clean_path } "
82
82
83
83
84
+ # pylint: disable=too-many-instance-attributes
84
85
@dataclass
85
86
class Endpoint :
86
87
"""
@@ -244,10 +245,32 @@ def _add_responses(
244
245
endpoint .responses .append (response )
245
246
return endpoint , schemas
246
247
248
+ # pylint: disable=too-many-return-statements
247
249
@staticmethod
248
250
def add_parameters (
249
251
* , endpoint : "Endpoint" , data : Union [oai .Operation , oai .PathItem ], schemas : Schemas , config : Config
250
252
) -> Tuple [Union ["Endpoint" , ParseError ], Schemas ]:
253
+ """Process the defined `parameters` for an Endpoint.
254
+
255
+ Any existing parameters will be ignored, so earlier instances of a parameter take precedence. PathItem
256
+ parameters should therefore be added __after__ operation parameters.
257
+
258
+ Args:
259
+ endpoint: The endpoint to add parameters to.
260
+ data: The Operation or PathItem to add parameters from.
261
+ schemas: The cumulative Schemas of processing so far which should contain details for any references.
262
+ config: User-provided config for overrides within parameters.
263
+
264
+ Returns:
265
+ `(result, schemas)` where `result` is either an updated Endpoint containing the parameters or a ParseError
266
+ describing what went wrong. `schemas` is an updated version of the `schemas` input, adding any new enums
267
+ or classes.
268
+
269
+ See Also:
270
+ - https://swagger.io/docs/specification/describing-parameters/
271
+ - https://swagger.io/docs/specification/paths-and-operations/
272
+ """
273
+
251
274
endpoint = deepcopy (endpoint )
252
275
if data .parameters is None :
253
276
return endpoint , schemas
@@ -329,6 +352,16 @@ def add_parameters(
329
352
330
353
@staticmethod
331
354
def sort_parameters (* , endpoint : "Endpoint" ) -> Union ["Endpoint" , ParseError ]:
355
+ """
356
+ Sorts the path parameters of an `endpoint` so that they match the order declared in `endpoint.path`.
357
+
358
+ Args:
359
+ endpoint: The endpoint to sort the parameters of.
360
+
361
+ Returns:
362
+ Either an updated `endpoint` with sorted path parameters or a `ParseError` if something was wrong with
363
+ the path parameters and they could not be sorted.
364
+ """
332
365
endpoint = deepcopy (endpoint )
333
366
parameters_from_path = re .findall (_PATH_PARAM_REGEX , endpoint .path )
334
367
try :
@@ -338,8 +371,8 @@ def sort_parameters(*, endpoint: "Endpoint") -> Union["Endpoint", ParseError]:
338
371
endpoint .path_parameters = OrderedDict ((param .name , param ) for param in sorted_params )
339
372
except ValueError :
340
373
pass # We're going to catch the difference down below
341
- path_parameter_names = [ name for name in endpoint . path_parameters ]
342
- if parameters_from_path != path_parameter_names :
374
+
375
+ if parameters_from_path != list ( endpoint . path_parameters ) :
343
376
return ParseError (
344
377
detail = f"Incorrect path templating for { endpoint .path } (Path parameters do not match with path)" ,
345
378
)
@@ -397,22 +430,17 @@ class GeneratorData:
397
430
enums : Iterator [EnumProperty ]
398
431
399
432
@staticmethod
400
- def from_dict (d : Dict [str , Any ], * , config : Config ) -> Union ["GeneratorData" , GeneratorError ]:
433
+ def from_dict (data : Dict [str , Any ], * , config : Config ) -> Union ["GeneratorData" , GeneratorError ]:
401
434
"""Create an OpenAPI from dict"""
402
435
try :
403
- openapi = oai .OpenAPI .parse_obj (d )
404
- except ValidationError as e :
405
- detail = str (e )
406
- if "swagger" in d :
436
+ openapi = oai .OpenAPI .parse_obj (data )
437
+ except ValidationError as err :
438
+ detail = str (err )
439
+ if "swagger" in data :
407
440
detail = (
408
441
"You may be trying to use a Swagger document; this is not supported by this project.\n \n " + detail
409
442
)
410
443
return GeneratorError (header = "Failed to parse OpenAPI document" , detail = detail )
411
- if openapi .openapi .major != 3 :
412
- return GeneratorError (
413
- header = "openapi-python-client only supports OpenAPI 3.x" ,
414
- detail = f"The version of the provided document was { openapi .openapi } " ,
415
- )
416
444
schemas = Schemas ()
417
445
if openapi .components and openapi .components .schemas :
418
446
schemas = build_schemas (components = openapi .components .schemas , schemas = schemas , config = config )
0 commit comments