Skip to content

Commit 539ea83

Browse files
Copy parameter reference logic from upstream
Intead of ignoring reference-typed parameters, look them up in the Parameters object.
1 parent 93bc802 commit 539ea83

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

openapi_python_client/parser/openapi.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55

66
from pydantic import ValidationError
77

8+
from .properties.schemas import Parameters, parameter_from_reference
9+
810
from .. import schema as oai
911
from .. import utils
1012
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+
)
1222
from .reference import Reference
1323
from .responses import Response, response_from_data
1424

@@ -36,7 +46,7 @@ class EndpointCollection:
3646

3747
@staticmethod
3848
def from_data(
39-
*, data: Dict[str, oai.PathItem], schemas: Schemas
49+
*, data: Dict[str, oai.PathItem], schemas: Schemas, parameters: Parameters
4050
) -> Tuple[Dict[str, "EndpointCollection"], Schemas]:
4151
""" Parse the openapi paths data to get EndpointCollections by tag """
4252
endpoints_by_tag: Dict[str, EndpointCollection] = {}
@@ -51,7 +61,7 @@ def from_data(
5161
tag = (operation.tags or ["default"])[0]
5262
collection = endpoints_by_tag.setdefault(tag, EndpointCollection(tag=tag))
5363
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
5565
)
5666
if isinstance(endpoint, ParseError):
5767
endpoint.header = (
@@ -217,14 +227,26 @@ def _add_responses(*, endpoint: "Endpoint", data: oai.Responses, schemas: Schema
217227

218228
@staticmethod
219229
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,
221235
) -> Tuple[Union["Endpoint", ParseError], Schemas]:
222236
endpoint = deepcopy(endpoint)
223237
if data.parameters is None:
224238
return endpoint, schemas
239+
225240
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:
227248
continue
249+
228250
prop, schemas = property_from_data(
229251
name=param.name,
230252
required=param.required,
@@ -248,7 +270,7 @@ def _add_parameters(
248270

249271
@staticmethod
250272
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
252274
) -> Tuple[Union["Endpoint", ParseError], Schemas]:
253275
""" Construct an endpoint from the OpenAPI data """
254276

@@ -266,7 +288,7 @@ def from_data(
266288
tag=tag,
267289
)
268290

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)
270292
if isinstance(result, ParseError):
271293
return result, schemas
272294
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
298320
schemas = Schemas()
299321
else:
300322
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+
)
302330
enums = schemas.enums
303331

304332
return GeneratorData(

0 commit comments

Comments
 (0)