Skip to content

Move Unmarshallers to separate subpackage #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions openapi_core/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""OpenAPI core exceptions module"""


class OpenAPIError(Exception):
pass
5 changes: 1 addition & 4 deletions openapi_core/schema/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""OpenAPI core schema exceptions module"""


class OpenAPIError(Exception):
pass
from openapi_core.exceptions import OpenAPIError


class OpenAPIMappingError(OpenAPIError):
Expand Down
Empty file.
16 changes: 16 additions & 0 deletions openapi_core/schema/extensions/generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""OpenAPI core extensions generators module"""
from six import iteritems

from openapi_core.schema.extensions.models import Extension


class ExtensionsGenerator(object):

def __init__(self, dereferencer):
self.dereferencer = dereferencer

def generate(self, item_spec):
for field_name, value in iteritems(item_spec):
if not field_name.startswith('x-'):
continue
yield field_name, Extension(field_name, value)
9 changes: 9 additions & 0 deletions openapi_core/schema/extensions/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""OpenAPI core extensions models module"""


class Extension(object):
"""Represents an OpenAPI Extension."""

def __init__(self, field_name, value=None):
self.field_name = field_name
self.value = value
3 changes: 2 additions & 1 deletion openapi_core/schema/media_types/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

from openapi_core.schema.media_types.exceptions import InvalidMediaTypeValue
from openapi_core.schema.schemas.exceptions import (
CastError, ValidateError, UnmarshalError,
CastError, ValidateError,
)
from openapi_core.unmarshalling.schemas.exceptions import UnmarshalError


MEDIA_TYPE_DESERIALIZERS = {
Expand Down
8 changes: 3 additions & 5 deletions openapi_core/schema/parameters/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
)
from openapi_core.schema.schemas.enums import SchemaType
from openapi_core.schema.schemas.exceptions import (
CastError, ValidateError, UnmarshalError,
CastError, ValidateError,
)
from openapi_core.unmarshalling.schemas.exceptions import UnmarshalError

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -81,10 +82,7 @@ def get_raw_value(self, request):
if self.required:
raise MissingRequiredParameter(self.name)

if not self.schema or self.schema.default is None:
raise MissingParameter(self.name)

return self.schema.default
raise MissingParameter(self.name)

if self.aslist and self.explode:
if hasattr(location, 'getall'):
Expand Down
66 changes: 0 additions & 66 deletions openapi_core/schema/schemas/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,6 @@ class ValidateError(OpenAPISchemaError):
pass


class UnmarshalError(OpenAPISchemaError):
"""Schema unmarshal operation error"""
pass


@attr.s(hash=True)
class UnmarshalValueError(UnmarshalError):
"""Failed to unmarshal value to type"""
value = attr.ib()
type = attr.ib()
original_exception = attr.ib(default=None)

def __str__(self):
return (
"Failed to unmarshal value {value} to type {type}: {exception}"
).format(
value=self.value, type=self.type,
exception=self.original_exception,
)


@attr.s(hash=True)
class InvalidSchemaValue(ValidateError):
value = attr.ib()
Expand All @@ -61,48 +40,3 @@ def __str__(self):
return (
"Value {value} not valid for schema of type {type}: {errors}"
).format(value=self.value, type=self.type, errors=self.schema_errors)


class UnmarshallerError(UnmarshalError):
"""Unmarshaller error"""
pass


@attr.s(hash=True)
class InvalidCustomFormatSchemaValue(UnmarshallerError):
"""Value failed to format with custom formatter"""
value = attr.ib()
type = attr.ib()
original_exception = attr.ib()

def __str__(self):
return (
"Failed to format value {value} to format {type}: {exception}"
).format(
value=self.value, type=self.type,
exception=self.original_exception,
)


@attr.s(hash=True)
class FormatterNotFoundError(UnmarshallerError):
"""Formatter not found to unmarshal"""
value = attr.ib()
type_format = attr.ib()

def __str__(self):
return (
"Formatter not found for {format} format "
"to unmarshal value {value}"
).format(format=self.type_format, value=self.value)


@attr.s(hash=True)
class UnmarshallerStrictTypeError(UnmarshallerError):
value = attr.ib()
types = attr.ib()

def __str__(self):
types = ', '.join(list(map(str, self.types)))
return "Value {value} is not one of types: {types}".format(
value=self.value, types=types)
16 changes: 12 additions & 4 deletions openapi_core/schema/schemas/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from six import iteritems

from openapi_core.compat import lru_cache
from openapi_core.schema.extensions.generators import ExtensionsGenerator
from openapi_core.schema.properties.generators import PropertiesGenerator
from openapi_core.schema.schemas.models import Schema
from openapi_core.schema.schemas.types import Contribution
from openapi_core.schema.schemas.types import Contribution, NoValue

log = logging.getLogger(__name__)

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

schema_type = schema_deref.get('type', None)
schema_format = schema_deref.get('format')
model = schema_deref.get('x-model', None)
required = schema_deref.get('required', False)
default = schema_deref.get('default', None)
default = schema_deref.get('default', NoValue)
properties_spec = schema_deref.get('properties', None)
items_spec = schema_deref.get('items', None)
nullable = schema_deref.get('nullable', False)
Expand All @@ -47,6 +47,8 @@ def create(self, schema_spec):
min_properties = schema_deref.get('minProperties', None)
max_properties = schema_deref.get('maxProperties', None)

extensions = self.extensions_generator.generate(schema_deref)

properties = None
if properties_spec:
properties = self.properties_generator.generate(properties_spec)
Expand All @@ -68,7 +70,7 @@ def create(self, schema_spec):
additional_properties = self.create(additional_properties_spec)

return Schema(
schema_type=schema_type, model=model, properties=properties,
schema_type=schema_type, properties=properties,
items=items, schema_format=schema_format, required=required,
default=default, nullable=nullable, enum=enum,
deprecated=deprecated, all_of=all_of, one_of=one_of,
Expand All @@ -79,9 +81,15 @@ def create(self, schema_spec):
exclusive_maximum=exclusive_maximum,
exclusive_minimum=exclusive_minimum,
min_properties=min_properties, max_properties=max_properties,
extensions=extensions,
_source=schema_deref,
)

@property
@lru_cache()
def extensions_generator(self):
return ExtensionsGenerator(self.dereferencer)

@property
@lru_cache()
def properties_generator(self):
Expand Down
Loading