-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathdeserializers.py
56 lines (48 loc) · 1.94 KB
/
deserializers.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
import warnings
from typing import Any
from typing import Callable
from typing import List
from typing import Optional
from jsonschema_path import SchemaPath
from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.deserializing.styles.datatypes import DeserializerCallable
from openapi_core.deserializing.styles.exceptions import (
EmptyQueryParameterValue,
)
from openapi_core.schema.parameters import get_aslist
from openapi_core.schema.parameters import get_explode
class CallableStyleDeserializer:
def __init__(
self,
param_or_header: SchemaPath,
style: str,
deserializer_callable: Optional[DeserializerCallable] = None,
):
self.param_or_header = param_or_header
self.style = style
self.deserializer_callable = deserializer_callable
self.aslist = get_aslist(self.param_or_header)
self.explode = get_explode(self.param_or_header)
def deserialize(self, value: Any) -> Any:
if self.deserializer_callable is None:
warnings.warn(f"Unsupported {self.style} style")
return value
# if "in" not defined then it's a Header
if "allowEmptyValue" in self.param_or_header:
warnings.warn(
"Use of allowEmptyValue property is deprecated",
DeprecationWarning,
)
allow_empty_values = self.param_or_header.getkey(
"allowEmptyValue", False
)
location_name = self.param_or_header.getkey("in", "header")
if location_name == "query" and value == "" and not allow_empty_values:
name = self.param_or_header["name"]
raise EmptyQueryParameterValue(name)
if not self.aslist or self.explode:
return value
try:
return self.deserializer_callable(value)
except (ValueError, TypeError, AttributeError):
raise DeserializeError(location_name, self.style, value)