Skip to content

Commit facebd5

Browse files
committed
Unmarshallers refactor
1 parent 3ffe2bc commit facebd5

File tree

6 files changed

+158
-164
lines changed

6 files changed

+158
-164
lines changed

openapi_core/schema/schemas.py

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from collections import namedtuple
12
from typing import Dict
23
from typing import Optional
34

45
from openapi_core.unmarshalling.schemas.formatters import Formatter
56

67
CustomFormattersDict = Dict[str, Formatter]
78
FormattersDict = Dict[Optional[str], Formatter]
9+
SchemaUnmarshaller = namedtuple(
10+
"SchemaUnmarshaller",
11+
["schema", "unmarshaller"],
12+
)

openapi_core/unmarshalling/schemas/factories.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,11 @@ def create(
102102
if schema_type in self.COMPLEX_UNMARSHALLERS:
103103
complex_klass = self.COMPLEX_UNMARSHALLERS[schema_type]
104104
return complex_klass(
105-
schema, validator, formatter, self, context=self.context
105+
schema, validator, self, formatter, context=self.context
106106
)
107107

108108
klass = self.UNMARSHALLERS[schema_type]
109-
return klass(schema, validator, formatter)
110-
111-
def get_formatter(
112-
self, type_format: str, default_formatters: FormattersDict
113-
) -> Optional[Formatter]:
114-
try:
115-
return self.custom_formatters[type_format]
116-
except KeyError:
117-
return default_formatters.get(type_format)
109+
return klass(schema, validator, self, formatter)
118110

119111
def get_validator(self, schema: Spec) -> Validator:
120112
resolver = schema.accessor.resolver # type: ignore

openapi_core/unmarshalling/schemas/formatters.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ class Formatter:
88
def validate(self, value: Any) -> bool:
99
return True
1010

11-
def unmarshal(self, value: Any) -> Any:
11+
def format(self, value: Any) -> Any:
1212
return value
1313

1414
@classmethod
1515
def from_callables(
1616
cls,
17-
validate: Optional[Callable[[Any], Any]] = None,
18-
unmarshal: Optional[Callable[[Any], Any]] = None,
17+
validate_callable: Optional[Callable[[Any], Any]] = None,
18+
format_callable: Optional[Callable[[Any], Any]] = None,
1919
) -> "Formatter":
2020
attrs = {}
21-
if validate is not None:
22-
attrs["validate"] = staticmethod(validate)
23-
if unmarshal is not None:
24-
attrs["unmarshal"] = staticmethod(unmarshal)
21+
if validate_callable is not None:
22+
attrs["validate"] = staticmethod(validate_callable)
23+
if format_callable is not None:
24+
attrs["format"] = staticmethod(format_callable)
2525

2626
klass: Type[Formatter] = type("Formatter", (cls,), attrs)
2727
return klass()

0 commit comments

Comments
 (0)