/
parameter.py
350 lines (296 loc) · 12.4 KB
/
parameter.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import enum
import datetime
import decimal
import typing
import uuid
import json
from typing import Union, Optional, Dict, Any
from collections.abc import MutableMapping
from pydantic import BaseModel, Field, model_validator
import more_itertools
from ..base import ObjectExtended, ParameterBase as ParameterBase_, ReferenceBase
from ..errors import ParameterFormatError
from .example import Example
from .general import Reference
from .schemas import Schema
from ..model import TYPES_SCHEMA_MAP
if typing.TYPE_CHECKING:
from .paths import MediaType
from .._types import v3xSchemaType
class _ParameterCodec:
def _codec(self):
if self.in_ == "path":
style = self.style or "simple"
assert style in frozenset(["matrix", "label", "simple"])
explode = self.explode or False
elif self.in_ == "header":
style = self.style or "simple"
assert style in frozenset(["simple"])
explode = False
elif self.in_ == "query":
style = self.style or "form"
assert style in frozenset(["form", "spaceDelimited", "pipeDelimited", "deepObject"])
explode = self.explode if self.explode is not None else (False if style != "form" else True)
elif self.in_ == "cookie":
style = self.style or "form"
assert style in frozenset(["form"])
explode = self.explode if self.explode is not None else (False if style != "form" else True)
else:
raise ParameterFormatError(self)
schema = self.schema_ or self.content.get("application/json").schema_
if isinstance(schema, ReferenceBase):
schema = schema._target
return schema, style, explode
def _encode(self, name: str, value):
schema, style, explode = self._codec()
value = schema.model(value)
if isinstance(value, BaseModel):
type_ = "object"
elif (t := type(value)) in (
bytes,
datetime.datetime,
datetime.date,
datetime.time,
datetime.timedelta,
uuid.UUID,
):
type_ = "string"
elif t in TYPES_SCHEMA_MAP:
type_ = TYPES_SCHEMA_MAP[t]
else:
raise TypeError(f"Unsupported type {t}")
return self._encode_value(name, type_, value, schema, explode, style)
def _encode_value(self, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool, style: str):
f = getattr(self, f"_encode__{style}")
return f(name, type_, value, schema, explode)
def _encode__matrix(self, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool):
"""
3.2.7. Path-Style Parameter Expansion: {;var}
https://www.rfc-editor.org/rfc/rfc6570#section-3.2.8
:param type_:
"""
if type_ in frozenset(["string", "number", "integer"]):
# ;color=blue
value = f";{name}={value}"
elif type_ == "boolean":
value = f";{name}={json.dumps(value)}"
elif type_ == "null":
value = f";{name}"
elif type_ == "array":
if explode is False:
# ;color=blue,black,brown
value = f";{name}={','.join(value)}"
else:
# ;color=blue;color=black;color=brown
value = "".join([f";{name}={v}" for v in value])
elif type_ == "object":
values = value if isinstance(value, dict) else value.model_dump(exclude_unset=True)
value = ",".join([f"{k},{v}" for k, v in values.items()])
if explode is False:
# ;color=R,100,G,200,B,150
value = f";{name}={value}"
else:
# ;R=100;G=200;B=150
value = f";{value}"
return {name: value}
def _encode__label(self, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool):
"""
3.2.5. Label Expansion with Dot-Prefix: {.var}
https://www.rfc-editor.org/rfc/rfc6570#section-3.2.5
"""
if type_ == "string":
# .blue
value = f".{value}"
elif type_ in frozenset(["number", "integer"]):
value = f".{str(value)}"
elif type_ == "boolean":
value = f".{json.dumps(value)}"
elif type_ == "null":
value = "."
elif type_ == "array":
# .blue.black.brown
value = "." + ".".join(value)
elif type_ == "object":
values = value if isinstance(value, dict) else value.model_dump(exclude_unset=True)
if explode:
# .R=100.G=200.B=150
value = ".".join([f"{k}={v}" for k, v in values.items()])
else:
# .R.100.G.200.B.150
value = ".".join([f"{k}.{v}" for k, v in values.items()])
value = "." + value
else:
raise ValueError(type_)
return {name: value}
def _encode__form(self, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool):
"""
https://spec.openapis.org/oas/v3.1.0#style-examples
3.2.8. Form-Style Query Expansion: {?var}
3.2.9. Form-Style Query Continuation: {&var}
https://www.rfc-editor.org/rfc/rfc6570#section-3.2.8
"""
if type_ == "string":
# color=blue
return {name: value}
elif type_ in frozenset(["number", "integer"]):
return {name: str(value)}
elif type_ == "boolean":
return {name: json.dumps(value)}
elif type_ == "null":
return {name: ""}
elif type_ == "array":
assert isinstance(value, (list, tuple))
if explode is False:
# color=blue,black,brown
value = ",".join(map(str, value))
else:
# color=blue&color=black&color=brown
pass
return {name: value}
elif type_ == "object":
values = value if isinstance(value, dict) else value.model_dump(exclude_unset=True)
if explode is False:
# color=R,100,G,200,B,150
value = ",".join([f"{k},{v}" for k, v in values.items()])
return {name: value}
else:
# R=100&G=200&B=150
return values
else:
raise ValueError(type_)
def _encode__simple(self, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool):
"""
3.2.2. Simple String Expansion: {var}
https://www.rfc-editor.org/rfc/rfc6570#section-3.2.2
"""
if type_ == "string":
return {name: value}
elif type_ in frozenset(["number", "integer"]):
return {name: str(value)}
elif type_ == "boolean":
return {name: json.dumps(value)}
elif type_ == "null":
return dict()
elif type_ == "array":
assert isinstance(value, (list, tuple))
# blue,black,brown
value = ",".join(map(str, value))
return {name: value}
elif type_ == "object":
values = value if isinstance(value, dict) else value.model_dump(exclude_unset=True)
if explode is False:
# R,100,G,200,B,150
value = ",".join([f"{k},{v}" for k, v in values.items()])
else:
# R=100,G=200,B=150
value = ",".join([f"{k}={v}" for k, v in values.items()])
return {name: value}
else:
raise ValueError(type_)
def _encode__spaceDelimited(self, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool):
# blue%20black%20brown
# R%20100%20G%20200%20B%20150
return self._encode__Delimited(" ", name, type_, value, schema, explode)
def _encode__pipeDelimited(self, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool):
# blue|black|brown
# R|100|G|200|B|150
return self._encode__Delimited("|", name, type_, value, schema, explode)
def _encode__Delimited(self, sep: str, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool):
assert explode is False
if value is None:
return dict()
if type_ == "array":
value = sep.join(value)
elif type_ == "object":
values = value if isinstance(value, dict) else value.model_dump(exclude_unset=True)
value = sep.join([f"{k}{sep}{v}" for k, v in values.items()])
else:
raise ValueError(type_)
return {name: value}
def _encode__deepObject(self, name: str, type_: str, value, schema: "v3xSchemaType", explode: bool):
assert type_ == "object" and explode is True
if not value:
return dict()
values = value if isinstance(value, dict) else value.model_dump()
# color[R]=100&color[G]=200&color[B]=150
def _flatten_dict(data, key_):
for k, v in data.items():
key = f"{key_}[{k}]"
if isinstance(v, MutableMapping):
yield from flatten_dict(v, key).items()
else:
yield key, v
def flatten_dict(d: MutableMapping, key: str = ""):
return dict(_flatten_dict(d, key))
values = {k: v for k, v in flatten_dict(values, name).items()}
return values
def _decode(self, value):
schema, style, explode = self._codec()
if style == "simple":
return self._decode_simple(value, schema, explode)
else:
raise ValueError(f"style {style} can not be decoded")
def _decode_simple(self, value, schema: "v3xSchemaType", explode: bool):
if schema.type == "array":
return value.split(",")
elif schema.type == "object":
if explode is False:
# R,100,G,200,B,150
return dict(more_itertools.chunked(value.split(","), 2))
else:
# R=100,G=200,B=150
return dict(map(lambda y: (y[0], y[2]), map(lambda x: x.partition("="), value.split(","))))
else:
# convert basic type
return value
class ParameterBase(ObjectExtended, ParameterBase_):
"""
A `Parameter Object`_ defines a single operation parameter.
.. _Parameter Object: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#external-documentation-object
"""
description: Optional[str] = Field(default=None)
required: Optional[bool] = Field(default=None)
deprecated: Optional[bool] = Field(default=None)
allowEmptyValue: Optional[bool] = Field(default=None)
style: Optional[str] = Field(default=None)
explode: Optional[bool] = Field(default=None)
allowReserved: Optional[bool] = Field(default=None)
schema_: Optional[Union[Schema, Reference]] = Field(default=None, alias="schema")
example: Optional[Any] = Field(default=None)
examples: Dict[str, Union["Example", Reference]] = Field(default_factory=dict)
content: Dict[str, "MediaType"] = Field(default_factory=dict)
class _In(str, enum.Enum):
query = "query"
header = "header"
path = "path"
cookie = "cookie"
class Parameter(ParameterBase, _ParameterCodec):
name: str = Field()
in_: _In = Field(alias="in") # TODO must be one of ["query","header","path","cookie"]
@model_validator(mode="after")
def validate_Parameter(cls, p: "ParameterBase"):
assert p.in_ != "path" or p.required is True, "Parameter '%s' must be required since it is in the path" % p.name
return p
def encode_parameter(
name: str,
value: object,
style: Optional[str],
explode: Optional[bool],
allowReserved: Optional[bool],
in_: str,
schema_: Schema,
) -> Union[str, bytes]:
p = Parameter(name=name, style=style, explode=explode, allowReserved=allowReserved, **{"in": in_, "schema": None})
p.schema_ = schema_
r = p._encode(name, value)[name]
if isinstance(r, (int, float, decimal.Decimal, datetime.datetime, datetime.date, datetime.time, uuid.UUID)):
r = str(r)
assert isinstance(r, (str, bytes))
return r
class Header(ParameterBase, _ParameterCodec):
"""
.. _HeaderObject: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#header-object
"""
def _codec(self):
schema = self.schema_ or self.content.get("application/json").schema_
return schema, "simple", False