Skip to content

Commit 93bc802

Browse files
Copy build_parameters from upstream
This builds the actual Parameters object from the OpenAPI spec, using the logic we just added to schemas.
1 parent 05eeb3f commit 93bc802

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

openapi_python_client/parser/properties/__init__.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
from itertools import chain
44
from typing import Any, ClassVar, Dict, Generic, Iterable, Iterator, List, Optional, Set, Tuple, TypeVar, Union
5+
from xml.etree.ElementTree import ParseError
56

67
import attr
78

89
from ... import schema as oai
910
from ... import utils
10-
from ..errors import PropertyError, ValidationError
11+
from ..errors import ParameterError, PropertyError, ValidationError
1112
from ..reference import Reference
1213
from .converter import convert, convert_chain
1314
from .enum_property import EnumProperty
1415
from .model_property import ModelProperty
1516
from .property import Property
16-
from .schemas import Schemas
17+
from .schemas import Parameters, Schemas, parse_reference_path, update_parameters_with_data
1718

1819

1920
@attr.s(auto_attribs=True, frozen=True)
@@ -671,3 +672,44 @@ def build_schemas(*, components: Dict[str, Union[oai.Reference, oai.Schema]]) ->
671672
schemas.errors.extend(errors)
672673
schemas.errors.extend(resolve_errors)
673674
return schemas
675+
676+
677+
def build_parameters(
678+
*,
679+
components: Dict[str, Union[oai.Reference, oai.Parameter]],
680+
) -> Parameters:
681+
"""Get a list of Parameters from an OpenAPI dict"""
682+
parameters = Parameters()
683+
to_process: Iterable[Tuple[str, Union[oai.Reference, oai.Parameter]]] = []
684+
if components is not None:
685+
to_process = components.items()
686+
still_making_progress = True
687+
errors: list[ParameterError] = []
688+
689+
# References could have forward References so keep going as long as we are making progress
690+
while still_making_progress:
691+
still_making_progress = False
692+
errors = []
693+
next_round = []
694+
# Only accumulate errors from the last round, since we might fix some along the way
695+
for name, data in to_process:
696+
if isinstance(data, oai.Reference):
697+
parameters.errors.append(ParameterError(data=data, detail="Reference parameters are not supported."))
698+
continue
699+
ref_path = parse_reference_path(f"#/components/parameters/{name}")
700+
if isinstance(ref_path, ParseError):
701+
parameters.errors.append(ParameterError(detail=ref_path.detail, data=data))
702+
continue
703+
parameters_or_err = update_parameters_with_data(
704+
ref_path=ref_path, data=data, parameters=parameters
705+
)
706+
if isinstance(parameters_or_err, ParameterError):
707+
next_round.append((name, data))
708+
errors.append(parameters_or_err)
709+
continue
710+
parameters = parameters_or_err
711+
still_making_progress = True
712+
to_process = next_round
713+
714+
parameters.errors.extend(errors)
715+
return parameters

0 commit comments

Comments
 (0)