Skip to content

Commit b026fb2

Browse files
committed
Move Unmarshallers to separate subpackage
1 parent 745736b commit b026fb2

File tree

23 files changed

+537
-468
lines changed

23 files changed

+537
-468
lines changed

openapi_core/exceptions.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""OpenAPI core exceptions module"""
2+
3+
4+
class OpenAPIError(Exception):
5+
pass

openapi_core/schema/exceptions.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
"""OpenAPI core schema exceptions module"""
2-
3-
4-
class OpenAPIError(Exception):
5-
pass
2+
from openapi_core.exceptions import OpenAPIError
63

74

85
class OpenAPIMappingError(OpenAPIError):

openapi_core/schema/extensions/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""OpenAPI core extensions generators module"""
2+
from six import iteritems
3+
4+
from openapi_core.schema.extensions.models import Extension
5+
6+
7+
class ExtensionsGenerator(object):
8+
9+
def __init__(self, dereferencer):
10+
self.dereferencer = dereferencer
11+
12+
def generate(self, item_spec):
13+
for field_name, value in iteritems(item_spec):
14+
if not field_name.startswith('x-'):
15+
continue
16+
yield field_name, Extension(field_name, value)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""OpenAPI core extensions models module"""
2+
3+
4+
class Extension(object):
5+
"""Represents an OpenAPI Extension."""
6+
7+
def __init__(self, field_name, value=None):
8+
self.field_name = field_name
9+
self.value = value

openapi_core/schema/media_types/models.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
from openapi_core.schema.media_types.exceptions import InvalidMediaTypeValue
77
from openapi_core.schema.schemas.exceptions import (
8-
CastError, ValidateError, UnmarshalError,
8+
CastError, ValidateError,
99
)
10+
from openapi_core.unmarshalling.schemas.exceptions import UnmarshalError
1011

1112

1213
MEDIA_TYPE_DESERIALIZERS = {

openapi_core/schema/parameters/models.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
)
1212
from openapi_core.schema.schemas.enums import SchemaType
1313
from openapi_core.schema.schemas.exceptions import (
14-
CastError, ValidateError, UnmarshalError,
14+
CastError, ValidateError,
1515
)
16+
from openapi_core.unmarshalling.schemas.exceptions import UnmarshalError
1617

1718
log = logging.getLogger(__name__)
1819

@@ -81,10 +82,7 @@ def get_raw_value(self, request):
8182
if self.required:
8283
raise MissingRequiredParameter(self.name)
8384

84-
if not self.schema or self.schema.default is None:
85-
raise MissingParameter(self.name)
86-
87-
return self.schema.default
85+
raise MissingParameter(self.name)
8886

8987
if self.aslist and self.explode:
9088
if hasattr(location, 'getall'):

openapi_core/schema/schemas/exceptions.py

-66
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,6 @@ class ValidateError(OpenAPISchemaError):
2323
pass
2424

2525

26-
class UnmarshalError(OpenAPISchemaError):
27-
"""Schema unmarshal operation error"""
28-
pass
29-
30-
31-
@attr.s(hash=True)
32-
class UnmarshalValueError(UnmarshalError):
33-
"""Failed to unmarshal value to type"""
34-
value = attr.ib()
35-
type = attr.ib()
36-
original_exception = attr.ib(default=None)
37-
38-
def __str__(self):
39-
return (
40-
"Failed to unmarshal value {value} to type {type}: {exception}"
41-
).format(
42-
value=self.value, type=self.type,
43-
exception=self.original_exception,
44-
)
45-
46-
4726
@attr.s(hash=True)
4827
class InvalidSchemaValue(ValidateError):
4928
value = attr.ib()
@@ -61,48 +40,3 @@ def __str__(self):
6140
return (
6241
"Value {value} not valid for schema of type {type}: {errors}"
6342
).format(value=self.value, type=self.type, errors=self.schema_errors)
64-
65-
66-
class UnmarshallerError(UnmarshalError):
67-
"""Unmarshaller error"""
68-
pass
69-
70-
71-
@attr.s(hash=True)
72-
class InvalidCustomFormatSchemaValue(UnmarshallerError):
73-
"""Value failed to format with custom formatter"""
74-
value = attr.ib()
75-
type = attr.ib()
76-
original_exception = attr.ib()
77-
78-
def __str__(self):
79-
return (
80-
"Failed to format value {value} to format {type}: {exception}"
81-
).format(
82-
value=self.value, type=self.type,
83-
exception=self.original_exception,
84-
)
85-
86-
87-
@attr.s(hash=True)
88-
class FormatterNotFoundError(UnmarshallerError):
89-
"""Formatter not found to unmarshal"""
90-
value = attr.ib()
91-
type_format = attr.ib()
92-
93-
def __str__(self):
94-
return (
95-
"Formatter not found for {format} format "
96-
"to unmarshal value {value}"
97-
).format(format=self.type_format, value=self.value)
98-
99-
100-
@attr.s(hash=True)
101-
class UnmarshallerStrictTypeError(UnmarshallerError):
102-
value = attr.ib()
103-
types = attr.ib()
104-
105-
def __str__(self):
106-
types = ', '.join(list(map(str, self.types)))
107-
return "Value {value} is not one of types: {types}".format(
108-
value=self.value, types=types)

openapi_core/schema/schemas/factories.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
from six import iteritems
55

66
from openapi_core.compat import lru_cache
7+
from openapi_core.schema.extensions.generators import ExtensionsGenerator
78
from openapi_core.schema.properties.generators import PropertiesGenerator
89
from openapi_core.schema.schemas.models import Schema
9-
from openapi_core.schema.schemas.types import Contribution
10+
from openapi_core.schema.schemas.types import Contribution, NoValue
1011

1112
log = logging.getLogger(__name__)
1213

@@ -21,9 +22,8 @@ def create(self, schema_spec):
2122

2223
schema_type = schema_deref.get('type', None)
2324
schema_format = schema_deref.get('format')
24-
model = schema_deref.get('x-model', None)
2525
required = schema_deref.get('required', False)
26-
default = schema_deref.get('default', None)
26+
default = schema_deref.get('default', NoValue)
2727
properties_spec = schema_deref.get('properties', None)
2828
items_spec = schema_deref.get('items', None)
2929
nullable = schema_deref.get('nullable', False)
@@ -47,6 +47,8 @@ def create(self, schema_spec):
4747
min_properties = schema_deref.get('minProperties', None)
4848
max_properties = schema_deref.get('maxProperties', None)
4949

50+
extensions = self.extensions_generator.generate(schema_deref)
51+
5052
properties = None
5153
if properties_spec:
5254
properties = self.properties_generator.generate(properties_spec)
@@ -68,7 +70,7 @@ def create(self, schema_spec):
6870
additional_properties = self.create(additional_properties_spec)
6971

7072
return Schema(
71-
schema_type=schema_type, model=model, properties=properties,
73+
schema_type=schema_type, properties=properties,
7274
items=items, schema_format=schema_format, required=required,
7375
default=default, nullable=nullable, enum=enum,
7476
deprecated=deprecated, all_of=all_of, one_of=one_of,
@@ -79,9 +81,15 @@ def create(self, schema_spec):
7981
exclusive_maximum=exclusive_maximum,
8082
exclusive_minimum=exclusive_minimum,
8183
min_properties=min_properties, max_properties=max_properties,
84+
extensions=extensions,
8285
_source=schema_deref,
8386
)
8487

88+
@property
89+
@lru_cache()
90+
def extensions_generator(self):
91+
return ExtensionsGenerator(self.dereferencer)
92+
8593
@property
8694
@lru_cache()
8795
def properties_generator(self):

0 commit comments

Comments
 (0)