5
5
6
6
from pydantic import ValidationError
7
7
8
+ from .properties .schemas import Parameters , parameter_from_reference
9
+
8
10
from .. import schema as oai
9
11
from .. import utils
10
12
from .errors import GeneratorError , ParseError , PropertyError
11
- from .properties import EnumProperty , ModelProperty , Property , Schemas , build_schemas , property_from_data
13
+ from .properties import (
14
+ EnumProperty ,
15
+ ModelProperty ,
16
+ Property ,
17
+ Schemas ,
18
+ build_parameters ,
19
+ build_schemas ,
20
+ property_from_data ,
21
+ )
12
22
from .reference import Reference
13
23
from .responses import Response , response_from_data
14
24
@@ -36,7 +46,7 @@ class EndpointCollection:
36
46
37
47
@staticmethod
38
48
def from_data (
39
- * , data : Dict [str , oai .PathItem ], schemas : Schemas
49
+ * , data : Dict [str , oai .PathItem ], schemas : Schemas , parameters : Parameters
40
50
) -> Tuple [Dict [str , "EndpointCollection" ], Schemas ]:
41
51
""" Parse the openapi paths data to get EndpointCollections by tag """
42
52
endpoints_by_tag : Dict [str , EndpointCollection ] = {}
@@ -51,7 +61,7 @@ def from_data(
51
61
tag = (operation .tags or ["default" ])[0 ]
52
62
collection = endpoints_by_tag .setdefault (tag , EndpointCollection (tag = tag ))
53
63
endpoint , schemas = Endpoint .from_data (
54
- data = operation , path = path , method = method , tag = tag , schemas = schemas
64
+ data = operation , path = path , method = method , tag = tag , schemas = schemas , parameters = parameters
55
65
)
56
66
if isinstance (endpoint , ParseError ):
57
67
endpoint .header = (
@@ -217,14 +227,26 @@ def _add_responses(*, endpoint: "Endpoint", data: oai.Responses, schemas: Schema
217
227
218
228
@staticmethod
219
229
def _add_parameters (
220
- * , endpoint : "Endpoint" , data : oai .Operation , schemas : Schemas
230
+ * ,
231
+ endpoint : "Endpoint" ,
232
+ data : oai .Operation ,
233
+ schemas : Schemas ,
234
+ parameters : Parameters ,
221
235
) -> Tuple [Union ["Endpoint" , ParseError ], Schemas ]:
222
236
endpoint = deepcopy (endpoint )
223
237
if data .parameters is None :
224
238
return endpoint , schemas
239
+
225
240
for param in data .parameters :
226
- if isinstance (param , oai .Reference ) or param .param_schema is None :
241
+ # Obtain the parameter from the reference or just the parameter itself
242
+ param_or_error = parameter_from_reference (param = param , parameters = parameters )
243
+ if isinstance (param_or_error , ParseError ):
244
+ return param_or_error , schemas
245
+ param = param_or_error # noqa: PLW2901
246
+
247
+ if param .param_schema is None :
227
248
continue
249
+
228
250
prop , schemas = property_from_data (
229
251
name = param .name ,
230
252
required = param .required ,
@@ -248,7 +270,7 @@ def _add_parameters(
248
270
249
271
@staticmethod
250
272
def from_data (
251
- * , data : oai .Operation , path : str , method : str , tag : str , schemas : Schemas
273
+ * , data : oai .Operation , path : str , method : str , tag : str , schemas : Schemas , parameters : Parameters
252
274
) -> Tuple [Union ["Endpoint" , ParseError ], Schemas ]:
253
275
""" Construct an endpoint from the OpenAPI data """
254
276
@@ -266,7 +288,7 @@ def from_data(
266
288
tag = tag ,
267
289
)
268
290
269
- result , schemas = Endpoint ._add_parameters (endpoint = endpoint , data = data , schemas = schemas )
291
+ result , schemas = Endpoint ._add_parameters (endpoint = endpoint , data = data , schemas = schemas , parameters = parameters )
270
292
if isinstance (result , ParseError ):
271
293
return result , schemas
272
294
result , schemas = Endpoint ._add_responses (endpoint = result , data = data .responses , schemas = schemas )
@@ -298,7 +320,13 @@ def from_dict(d: Dict[str, Dict[str, Any]]) -> Union["GeneratorData", GeneratorE
298
320
schemas = Schemas ()
299
321
else :
300
322
schemas = build_schemas (components = openapi .components .schemas )
301
- endpoint_collections_by_tag , schemas = EndpointCollection .from_data (data = openapi .paths , schemas = schemas )
323
+ if openapi .components is None or openapi .components .parameters is None :
324
+ parameters = Parameters ()
325
+ else :
326
+ parameters = build_parameters (components = openapi .components .parameters )
327
+ endpoint_collections_by_tag , schemas = EndpointCollection .from_data (
328
+ data = openapi .paths , schemas = schemas , parameters = parameters
329
+ )
302
330
enums = schemas .enums
303
331
304
332
return GeneratorData (
0 commit comments