Skip to content

Commit 53912d7

Browse files
author
Jordi Sanchez
committed
Fixes linter issues and a failing test with the latest changes.
1 parent 494ac3e commit 53912d7

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

openapi_python_client/parser/openapi.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
build_schemas,
2424
property_from_data,
2525
)
26-
from .properties.schemas import parse_reference_path
26+
from .properties.schemas import parameter_from_reference
2727
from .responses import Response, response_from_data
2828

2929
_PATH_PARAM_REGEX = re.compile("{([a-zA-Z_][a-zA-Z0-9_]*)}")
@@ -293,6 +293,8 @@ def add_parameters(
293293
- https://swagger.io/docs/specification/describing-parameters/
294294
- https://swagger.io/docs/specification/paths-and-operations/
295295
"""
296+
# pylint: disable=too-many-branches, too-many-locals
297+
# There isn't much value in breaking down this function further other than to satisfy the linter.
296298

297299
if data.parameters is None:
298300
return endpoint, schemas, parameters
@@ -307,35 +309,32 @@ def add_parameters(
307309
oai.ParameterLocation.COOKIE: endpoint.cookie_parameters,
308310
}
309311

310-
for _param in data.parameters:
311-
param: oai.Parameter
312+
for param in data.parameters:
313+
# Obtain the parameter from the reference or just the parameter itself
314+
param_or_error = parameter_from_reference(param=param, parameters=parameters)
315+
if isinstance(param_or_error, ParseError):
316+
return param_or_error, schemas, parameters
317+
param = param_or_error
312318

313-
if _param is None:
314-
return ParseError(data=data, detail="Null parameter provided."), schemas, parameters
315-
316-
if isinstance(_param, oai.Reference):
317-
ref_path = parse_reference_path(_param.ref)
318-
if isinstance(ref_path, ParseError):
319-
return ref_path, schemas, parameters
320-
_resolved_class = parameters.classes_by_reference.get(ref_path)
321-
if _resolved_class is None:
322-
return ParseError(data=data, detail=f"Reference `{ref_path}` not found."), schemas, parameters
323-
param = _resolved_class
324-
elif isinstance(_param, oai.Parameter):
325-
param = _param
319+
if param.param_schema is None:
320+
continue
326321

327322
unique_param = (param.name, param.param_in)
328-
if unique_param in unique_parameters:
329-
duplication_detail = (
330-
"Parameters MUST NOT contain duplicates. "
331-
"A unique parameter is defined by a combination of a name and location. "
332-
f"Duplicated parameters named `{param.name}` detected in `{param.param_in}`."
323+
if (param.name, param.param_in) in unique_parameters:
324+
return (
325+
ParseError(
326+
data=data,
327+
detail=(
328+
"Parameters MUST NOT contain duplicates. "
329+
"A unique parameter is defined by a combination of a name and location. "
330+
f"Duplicated parameters named `{param.name}` detected in `{param.param_in}`."
331+
),
332+
),
333+
schemas,
334+
parameters,
333335
)
334-
return ParseError(data=data, detail=duplication_detail), schemas, parameters
335-
unique_parameters.add(unique_param)
336336

337-
if param.param_schema is None:
338-
continue
337+
unique_parameters.add(unique_param)
339338

340339
prop, new_schemas = property_from_data(
341340
name=param.name,

openapi_python_client/parser/properties/schemas.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
__all__ = ["Class", "Schemas", "Parameters", "parse_reference_path", "update_schemas_with_data"]
1+
__all__ = [
2+
"Class",
3+
"Schemas",
4+
"Parameters",
5+
"parse_reference_path",
6+
"update_schemas_with_data",
7+
"parameter_from_reference",
8+
]
29

310
from typing import TYPE_CHECKING, Dict, List, NewType, Tuple, Union, cast
411
from urllib.parse import urlparse
@@ -190,3 +197,35 @@ def update_parameters_with_data(
190197

191198
parameters = attr.evolve(parameters, classes_by_reference={ref_path: param, **parameters.classes_by_reference})
192199
return parameters
200+
201+
202+
def parameter_from_reference(
203+
*,
204+
param: Union[oai.Reference, Parameter],
205+
parameters: Parameters,
206+
) -> Union[Parameter, ParameterError]:
207+
"""
208+
Returns a Parameter from a Reference or the Parameter itself if one was provided.
209+
210+
Args:
211+
param: A parameter by `Reference`.
212+
parameters: `Parameters` up until now.
213+
214+
Returns:
215+
Either the updated `schemas` input or a `PropertyError` if something went wrong.
216+
217+
See Also:
218+
- https://swagger.io/docs/specification/using-ref/
219+
"""
220+
if isinstance(param, Parameter):
221+
return param
222+
223+
ref_path = parse_reference_path(param.ref)
224+
225+
if isinstance(ref_path, ParseError):
226+
return ParameterError(detail=ref_path.detail)
227+
228+
_resolved_parameter_class = parameters.classes_by_reference.get(ref_path, None)
229+
if _resolved_parameter_class is None:
230+
return ParameterError(detail=f"Reference `{ref_path}` not found.")
231+
return _resolved_parameter_class

tests/test_parser/test_openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ def test__add_parameters_resolves_references(self, mocker, param_factory):
707707
)
708708

709709
parameters = mocker.MagicMock()
710-
new_param = param_factory(name="blah")
710+
new_param = param_factory(name="blah", schema=oai.Schema.construct(nullable=False, type="string"))
711711
parameters.classes_by_name = {
712712
"blah": new_param,
713713
}

0 commit comments

Comments
 (0)