Skip to content

Commit 82738c3

Browse files
authored
Merge pull request #349 from p1c2u/refactor/unmarshalling-refactor
Unmarshalling refactor
2 parents 8a99a63 + 3e9e310 commit 82738c3

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

openapi_core/unmarshalling/schemas/factories.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,25 @@ def create(self, schema, type_override=None):
5151
warnings.warn("The schema is deprecated", DeprecationWarning)
5252

5353
schema_type = type_override or schema.getkey('type', 'any')
54+
schema_format = schema.getkey('format')
5455

5556
klass = self.UNMARSHALLERS[schema_type]
56-
kwargs = dict(schema=schema)
57-
58-
if schema_type in self.COMPLEX_UNMARSHALLERS:
59-
kwargs.update(
60-
unmarshallers_factory=self,
61-
context=self.context,
62-
)
63-
64-
schema_format = schema.getkey('format')
65-
formatter = self.get_formatter(klass.FORMATTERS, schema_format)
6657

58+
formatter = self.get_formatter(schema_format, klass.FORMATTERS)
6759
if formatter is None:
6860
raise FormatterNotFoundError(schema_format)
6961

7062
validator = self.get_validator(schema)
7163

72-
return klass(formatter, validator, **kwargs)
64+
kwargs = dict()
65+
if schema_type in self.COMPLEX_UNMARSHALLERS:
66+
kwargs.update(
67+
unmarshallers_factory=self,
68+
context=self.context,
69+
)
70+
return klass(schema, formatter, validator, **kwargs)
7371

74-
def get_formatter(self, default_formatters, type_format=None):
72+
def get_formatter(self, type_format, default_formatters):
7573
try:
7674
return self.custom_formatters[type_format]
7775
except KeyError:

openapi_core/unmarshalling/schemas/unmarshallers.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
log = logging.getLogger(__name__)
2828

2929

30-
class PrimitiveTypeUnmarshaller:
30+
class BaseSchemaUnmarshaller:
3131

32-
FORMATTERS = {}
32+
FORMATTERS = {
33+
None: Formatter(),
34+
}
3335

34-
def __init__(self, formatter, validator, schema):
35-
self.formatter = formatter
36-
self.validator = validator
36+
def __init__(self, schema):
3737
self.schema = schema
3838

3939
def __call__(self, value):
@@ -44,6 +44,20 @@ def __call__(self, value):
4444

4545
return self.unmarshal(value)
4646

47+
def validate(self, value):
48+
raise NotImplementedError
49+
50+
def unmarshal(self, value):
51+
raise NotImplementedError
52+
53+
54+
class PrimitiveTypeUnmarshaller(BaseSchemaUnmarshaller):
55+
56+
def __init__(self, schema, formatter, validator):
57+
super().__init__(schema)
58+
self.formatter = formatter
59+
self.validator = validator
60+
4761
def _formatter_validate(self, value):
4862
result = self.formatter.validate(value)
4963
if not result:
@@ -123,9 +137,9 @@ class BooleanUnmarshaller(PrimitiveTypeUnmarshaller):
123137
class ComplexUnmarshaller(PrimitiveTypeUnmarshaller):
124138

125139
def __init__(
126-
self, formatter, validator, schema, unmarshallers_factory,
140+
self, schema, formatter, validator, unmarshallers_factory,
127141
context=None):
128-
super().__init__(formatter, validator, schema)
142+
super().__init__(schema, formatter, validator)
129143
self.unmarshallers_factory = unmarshallers_factory
130144
self.context = context
131145

@@ -242,10 +256,6 @@ def _unmarshal_properties(self, value, one_of_schema=None):
242256

243257
class AnyUnmarshaller(ComplexUnmarshaller):
244258

245-
FORMATTERS = {
246-
None: Formatter(),
247-
}
248-
249259
SCHEMA_TYPES_ORDER = [
250260
'object', 'array', 'boolean',
251261
'integer', 'number', 'string',

0 commit comments

Comments
 (0)