-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathparameter.py
More file actions
104 lines (83 loc) · 3.54 KB
/
parameter.py
File metadata and controls
104 lines (83 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Parameter builder for path and operation parameters."""
import logging
from typing import Any
from openapi_parser.builders.common import (
PropertyMeta,
extract_extension_attributes,
extract_typed_props,
)
from openapi_parser.builders.content import ContentBuilder
from openapi_parser.builders.schema import SchemaFactory
from openapi_parser.enumeration import (
CookieParameterStyle,
HeaderParameterStyle,
ParameterLocation,
PathParameterStyle,
QueryParameterStyle,
)
from openapi_parser.errors import ParserError
from openapi_parser.specification import Parameter
logger = logging.getLogger(__name__)
style_to_enum_map = {
ParameterLocation.HEADER: HeaderParameterStyle,
ParameterLocation.PATH: PathParameterStyle,
ParameterLocation.QUERY: QueryParameterStyle,
ParameterLocation.COOKIE: CookieParameterStyle,
}
default_styles_by_location = {
ParameterLocation.HEADER: HeaderParameterStyle.SIMPLE,
ParameterLocation.PATH: PathParameterStyle.SIMPLE,
ParameterLocation.QUERY: QueryParameterStyle.FORM,
ParameterLocation.COOKIE: CookieParameterStyle.FORM,
}
class ParameterBuilder:
"""Builds parameter objects from raw specification data."""
_schema_factory: SchemaFactory
_content_builder: ContentBuilder
def __init__(
self,
schema_factory: SchemaFactory,
content_builder: ContentBuilder,
) -> None:
"""Initialize parameter builder.
Args:
schema_factory: Factory for creating schema objects
content_builder: Builder for content objects
"""
self._schema_factory = schema_factory
self._content_builder = content_builder
def build_list(self, parameters: list[dict[str, Any]]) -> list[Parameter]:
"""Build a list of parameters from a list of raw dicts."""
return [self.build(parameter) for parameter in parameters]
def build(self, data: dict[str, Any]) -> Parameter:
"""Build a Parameter from a raw dict."""
parameter_name = data.get("name")
if parameter_name is None:
raise ParserError("Parameter is missing required 'name' property")
logger.debug(f"Parameter parsing [name={parameter_name}]")
attrs_map = {
"name": PropertyMeta(name="name", cast=str),
"location": PropertyMeta(name="in", cast=ParameterLocation),
"required": PropertyMeta(name="required", cast=bool),
"schema": PropertyMeta(name="schema", cast=self._schema_factory.create),
"content": PropertyMeta(
name="content",
cast=self._content_builder.build_list,
),
"description": PropertyMeta(name="description", cast=str),
"example": PropertyMeta(name="example", cast=None),
"examples": PropertyMeta(name="examples", cast=dict),
"deprecated": PropertyMeta(name="deprecated", cast=bool),
"explode": PropertyMeta(name="explode", cast=bool),
}
attrs = extract_typed_props(data, attrs_map)
if data.get("style"):
attrs["style"] = style_to_enum_map[attrs["location"]](data["style"])
else:
attrs["style"] = default_styles_by_location[attrs["location"]]
if not attrs.get("explode") and attrs["style"].value == "form":
attrs["explode"] = True
attrs["extensions"] = extract_extension_attributes(data)
if attrs["extensions"]:
logger.debug(f"Extracted custom properties [{attrs['extensions'].keys()}]")
return Parameter(**attrs)