-
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathfactories.py
More file actions
169 lines (150 loc) · 5.64 KB
/
factories.py
File metadata and controls
169 lines (150 loc) · 5.64 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
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
import sys
import warnings
from typing import Any
from typing import Dict
from typing import Iterable
from typing import Optional
from typing import Type
from typing import Union
if sys.version_info >= (3, 8):
from functools import cached_property
else:
from backports.cached_property import cached_property
from jsonschema.protocols import Validator
from openapi_schema_validator import OAS30Validator
from openapi_core.spec import Spec
from openapi_core.unmarshalling.schemas.datatypes import CustomFormattersDict
from openapi_core.unmarshalling.schemas.datatypes import FormattersDict
from openapi_core.unmarshalling.schemas.enums import ValidationContext
from openapi_core.unmarshalling.schemas.exceptions import (
FormatterNotFoundError,
)
from openapi_core.unmarshalling.schemas.formatters import Formatter
from openapi_core.unmarshalling.schemas.unmarshallers import AnyUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import ArrayUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import (
BaseSchemaUnmarshaller,
)
from openapi_core.unmarshalling.schemas.unmarshallers import (
BooleanUnmarshaller,
)
from openapi_core.unmarshalling.schemas.unmarshallers import (
ComplexUnmarshaller,
)
from openapi_core.unmarshalling.schemas.unmarshallers import (
IntegerUnmarshaller,
)
from openapi_core.unmarshalling.schemas.unmarshallers import (
MultiTypeUnmarshaller,
)
from openapi_core.unmarshalling.schemas.unmarshallers import NullUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import NumberUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import ObjectUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import StringUnmarshaller
from openapi_core.unmarshalling.schemas.util import build_format_checker
class SchemaValidatorsFactory:
CONTEXTS = {
ValidationContext.REQUEST: "write",
ValidationContext.RESPONSE: "read",
}
def __init__(
self,
schema_validator_class: Type[Validator],
custom_formatters: Optional[CustomFormattersDict] = None,
context: Optional[ValidationContext] = None,
):
self.schema_validator_class = schema_validator_class
if custom_formatters is None:
custom_formatters = {}
self.custom_formatters = custom_formatters
self.context = context
def create(self, schema: Spec) -> Validator:
resolver = schema.accessor.resolver # type: ignore
custom_format_checks = {
name: formatter.validate
for name, formatter in self.custom_formatters.items()
}
format_checker = build_format_checker(**custom_format_checks)
kwargs = {
"resolver": resolver,
"format_checker": format_checker,
}
if self.context is not None:
kwargs[self.CONTEXTS[self.context]] = True
with schema.open() as schema_dict:
return self.schema_validator_class(schema_dict, **kwargs)
class SchemaUnmarshallersFactory:
UNMARSHALLERS: Dict[str, Type[BaseSchemaUnmarshaller]] = {
"string": StringUnmarshaller,
"integer": IntegerUnmarshaller,
"number": NumberUnmarshaller,
"boolean": BooleanUnmarshaller,
"array": ArrayUnmarshaller,
"object": ObjectUnmarshaller,
"null": NullUnmarshaller,
"any": AnyUnmarshaller,
}
COMPLEX_UNMARSHALLERS: Dict[str, Type[ComplexUnmarshaller]] = {
"array": ArrayUnmarshaller,
"object": ObjectUnmarshaller,
"any": AnyUnmarshaller,
}
def __init__(
self,
schema_validator_class: Type[Validator],
custom_formatters: Optional[CustomFormattersDict] = None,
context: Optional[ValidationContext] = None,
):
self.schema_validator_class = schema_validator_class
if custom_formatters is None:
custom_formatters = {}
self.custom_formatters = custom_formatters
self.context = context
@cached_property
def validators_factory(self) -> SchemaValidatorsFactory:
return SchemaValidatorsFactory(
self.schema_validator_class,
self.custom_formatters,
self.context,
)
def create(
self, schema: Spec, type_override: Optional[str] = None
) -> BaseSchemaUnmarshaller:
"""Create unmarshaller from the schema."""
if schema is None:
raise TypeError("Invalid schema")
if schema.getkey("deprecated", False):
warnings.warn("The schema is deprecated", DeprecationWarning)
validator = self.validators_factory.create(schema)
schema_format = schema.getkey("format")
formatter = self.custom_formatters.get(schema_format)
schema_type = type_override or schema.getkey("type", "any")
if isinstance(schema_type, Iterable) and not isinstance(
schema_type, str
):
return MultiTypeUnmarshaller(
schema,
validator,
formatter,
self.validators_factory,
self,
context=self.context,
)
if schema_type in self.COMPLEX_UNMARSHALLERS:
complex_klass = self.COMPLEX_UNMARSHALLERS[schema_type]
return complex_klass(
schema,
validator,
formatter,
self.validators_factory,
self,
context=self.context,
)
klass = self.UNMARSHALLERS[schema_type]
return klass(
schema,
validator,
formatter,
self.validators_factory,
self,
)