|
2 | 2 |
|
3 | 3 | from itertools import chain
|
4 | 4 | from typing import Any, ClassVar, Dict, Generic, Iterable, Iterator, List, Optional, Set, Tuple, TypeVar, Union
|
| 5 | +from xml.etree.ElementTree import ParseError |
5 | 6 |
|
6 | 7 | import attr
|
7 | 8 |
|
8 | 9 | from ... import schema as oai
|
9 | 10 | from ... import utils
|
10 |
| -from ..errors import PropertyError, ValidationError |
| 11 | +from ..errors import ParameterError, PropertyError, ValidationError |
11 | 12 | from ..reference import Reference
|
12 | 13 | from .converter import convert, convert_chain
|
13 | 14 | from .enum_property import EnumProperty
|
14 | 15 | from .model_property import ModelProperty
|
15 | 16 | from .property import Property
|
16 |
| -from .schemas import Schemas |
| 17 | +from .schemas import Parameters, Schemas, parse_reference_path, update_parameters_with_data |
17 | 18 |
|
18 | 19 |
|
19 | 20 | @attr.s(auto_attribs=True, frozen=True)
|
@@ -671,3 +672,44 @@ def build_schemas(*, components: Dict[str, Union[oai.Reference, oai.Schema]]) ->
|
671 | 672 | schemas.errors.extend(errors)
|
672 | 673 | schemas.errors.extend(resolve_errors)
|
673 | 674 | 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