mirror of
https://github.com/correl/openapi-core.git
synced 2024-12-28 19:19:23 +00:00
Get rid of cast in schema
This commit is contained in:
parent
b4c10e847a
commit
7da1ea6b77
8 changed files with 42 additions and 30 deletions
|
@ -1,3 +1,4 @@
|
|||
from openapi_core.casting.schemas.exceptions import CastError
|
||||
from openapi_core.schema.schemas.types import NoValue
|
||||
|
||||
|
||||
|
@ -9,7 +10,10 @@ class PrimitiveCaster(object):
|
|||
def __call__(self, value):
|
||||
if value in (None, NoValue):
|
||||
return value
|
||||
return self.caster_callable(value)
|
||||
try:
|
||||
return self.caster_callable(value)
|
||||
except (ValueError, TypeError) as exc:
|
||||
raise CastError(exc, self.caster_callable)
|
||||
|
||||
|
||||
class DummyCaster(object):
|
||||
|
|
|
@ -90,14 +90,3 @@ class Schema(object):
|
|||
def get_all_properties_names(self):
|
||||
all_properties = self.get_all_properties()
|
||||
return set(all_properties.keys())
|
||||
|
||||
def cast(self, value):
|
||||
"""Cast value from string to schema type"""
|
||||
from openapi_core.casting.schemas.exceptions import CastError
|
||||
from openapi_core.casting.schemas.factories import SchemaCastersFactory
|
||||
casters_factory = SchemaCastersFactory()
|
||||
caster = casters_factory.create(self)
|
||||
try:
|
||||
return caster(value)
|
||||
except (ValueError, TypeError):
|
||||
raise CastError(value, self.type)
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
from itertools import chain
|
||||
from six import iteritems
|
||||
|
||||
from openapi_core.casting.schemas.exceptions import CastError
|
||||
from openapi_core.schema.media_types.exceptions import (
|
||||
InvalidMediaTypeValue, InvalidContentType,
|
||||
)
|
||||
|
@ -112,10 +113,11 @@ class RequestValidator(object):
|
|||
deserialised = self._deserialise(param, raw_value)
|
||||
except OpenAPIParameterError as exc:
|
||||
errors.append(exc)
|
||||
continue
|
||||
else:
|
||||
try:
|
||||
casted = self._cast(param, deserialised)
|
||||
except OpenAPIParameterError as exc:
|
||||
except CastError as exc:
|
||||
errors.append(exc)
|
||||
continue
|
||||
|
||||
|
@ -153,7 +155,7 @@ class RequestValidator(object):
|
|||
else:
|
||||
try:
|
||||
casted = self._cast(media_type, deserialised)
|
||||
except InvalidMediaTypeValue as exc:
|
||||
except CastError as exc:
|
||||
errors.append(exc)
|
||||
else:
|
||||
try:
|
||||
|
@ -167,7 +169,15 @@ class RequestValidator(object):
|
|||
return param_or_media_type.deserialise(value)
|
||||
|
||||
def _cast(self, param_or_media_type, value):
|
||||
return param_or_media_type.cast(value)
|
||||
# return param_or_media_type.cast(value)
|
||||
if not param_or_media_type.schema:
|
||||
return value
|
||||
|
||||
from openapi_core.casting.schemas.exceptions import CastError
|
||||
from openapi_core.casting.schemas.factories import SchemaCastersFactory
|
||||
casters_factory = SchemaCastersFactory()
|
||||
caster = casters_factory.create(param_or_media_type.schema)
|
||||
return caster(value)
|
||||
|
||||
def _unmarshal(self, param_or_media_type, value):
|
||||
if not param_or_media_type.schema:
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
"""OpenAPI core validation response validators module"""
|
||||
from openapi_core.casting.schemas.exceptions import CastError
|
||||
from openapi_core.schema.operations.exceptions import InvalidOperation
|
||||
from openapi_core.schema.media_types.exceptions import (
|
||||
InvalidMediaTypeValue, InvalidContentType,
|
||||
|
@ -87,7 +88,7 @@ class ResponseValidator(object):
|
|||
else:
|
||||
try:
|
||||
casted = self._cast(media_type, deserialised)
|
||||
except InvalidMediaTypeValue as exc:
|
||||
except CastError as exc:
|
||||
errors.append(exc)
|
||||
else:
|
||||
try:
|
||||
|
@ -109,7 +110,16 @@ class ResponseValidator(object):
|
|||
return param_or_media_type.deserialise(value)
|
||||
|
||||
def _cast(self, param_or_media_type, value):
|
||||
return param_or_media_type.cast(value)
|
||||
# return param_or_media_type.cast(value)
|
||||
if not param_or_media_type.schema:
|
||||
return value
|
||||
|
||||
from openapi_core.casting.schemas.exceptions import CastError
|
||||
from openapi_core.casting.schemas.factories import SchemaCastersFactory
|
||||
casters_factory = SchemaCastersFactory()
|
||||
caster = casters_factory.create(param_or_media_type.schema)
|
||||
return caster(value)
|
||||
|
||||
|
||||
def _unmarshal(self, param_or_media_type, value):
|
||||
if not param_or_media_type.schema:
|
||||
|
|
|
@ -100,14 +100,13 @@ class TestFlaskOpenAPIDecorator(object):
|
|||
'errors': [
|
||||
{
|
||||
'class': (
|
||||
"<class 'openapi_core.schema.parameters."
|
||||
"exceptions.InvalidParameterValue'>"
|
||||
"<class 'openapi_core.casting.schemas.exceptions."
|
||||
"CastError'>"
|
||||
),
|
||||
'status': 400,
|
||||
'title': (
|
||||
'Invalid parameter value for `id`: '
|
||||
'Failed to cast value invalidparameter to type '
|
||||
'SchemaType.INTEGER'
|
||||
"Failed to cast value invalid literal for int() with "
|
||||
"base 10: 'invalidparameter' to type <class 'int'>"
|
||||
)
|
||||
}
|
||||
]
|
||||
|
|
|
@ -87,14 +87,13 @@ class TestFlaskOpenAPIView(object):
|
|||
'errors': [
|
||||
{
|
||||
'class': (
|
||||
"<class 'openapi_core.schema.parameters."
|
||||
"exceptions.InvalidParameterValue'>"
|
||||
"<class 'openapi_core.casting.schemas.exceptions."
|
||||
"CastError'>"
|
||||
),
|
||||
'status': 400,
|
||||
'title': (
|
||||
'Invalid parameter value for `id`: '
|
||||
'Failed to cast value invalidparameter to type '
|
||||
'SchemaType.INTEGER'
|
||||
"Failed to cast value invalid literal for int() with "
|
||||
"base 10: 'invalidparameter' to type <class 'int'>"
|
||||
)
|
||||
}
|
||||
]
|
||||
|
|
|
@ -5,6 +5,7 @@ from base64 import b64encode
|
|||
from uuid import UUID
|
||||
from six import text_type
|
||||
|
||||
from openapi_core.casting.schemas.exceptions import CastError
|
||||
from openapi_core.extensions.models.models import BaseModel
|
||||
from openapi_core.schema.media_types.exceptions import InvalidContentType
|
||||
from openapi_core.schema.parameters.exceptions import (
|
||||
|
@ -291,7 +292,7 @@ class TestPetstore(object):
|
|||
path_pattern=path_pattern, args=query_params,
|
||||
)
|
||||
|
||||
with pytest.raises(InvalidParameterValue):
|
||||
with pytest.raises(CastError):
|
||||
validate_parameters(spec, request)
|
||||
|
||||
body = validate_body(spec, request)
|
||||
|
|
|
@ -3,13 +3,13 @@ import json
|
|||
import pytest
|
||||
from six import text_type
|
||||
|
||||
from openapi_core.casting.schemas.exceptions import CastError
|
||||
from openapi_core.schema.media_types.exceptions import (
|
||||
InvalidContentType, InvalidMediaTypeValue,
|
||||
)
|
||||
from openapi_core.extensions.models.models import BaseModel
|
||||
from openapi_core.schema.operations.exceptions import InvalidOperation
|
||||
from openapi_core.schema.parameters.exceptions import MissingRequiredParameter
|
||||
from openapi_core.schema.parameters.exceptions import InvalidParameterValue
|
||||
from openapi_core.schema.paths.exceptions import InvalidPath
|
||||
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
|
||||
from openapi_core.schema.responses.exceptions import (
|
||||
|
@ -317,7 +317,7 @@ class TestPathItemParamsValidator(object):
|
|||
result = validator.validate(request)
|
||||
|
||||
assert len(result.errors) == 1
|
||||
assert type(result.errors[0]) == InvalidParameterValue
|
||||
assert type(result.errors[0]) == CastError
|
||||
assert result.body is None
|
||||
assert result.parameters == RequestParameters()
|
||||
|
||||
|
|
Loading…
Reference in a new issue