Skip to content

Commit 7da1ea6

Browse files
committed
Get rid of cast in schema
1 parent b4c10e8 commit 7da1ea6

File tree

8 files changed

+42
-30
lines changed

8 files changed

+42
-30
lines changed

openapi_core/casting/schemas/casters.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from openapi_core.casting.schemas.exceptions import CastError
12
from openapi_core.schema.schemas.types import NoValue
23

34

@@ -9,7 +10,10 @@ def __init__(self, caster_callable):
910
def __call__(self, value):
1011
if value in (None, NoValue):
1112
return value
12-
return self.caster_callable(value)
13+
try:
14+
return self.caster_callable(value)
15+
except (ValueError, TypeError) as exc:
16+
raise CastError(exc, self.caster_callable)
1317

1418

1519
class DummyCaster(object):

openapi_core/schema/schemas/models.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,3 @@ def get_all_properties(self):
9090
def get_all_properties_names(self):
9191
all_properties = self.get_all_properties()
9292
return set(all_properties.keys())
93-
94-
def cast(self, value):
95-
"""Cast value from string to schema type"""
96-
from openapi_core.casting.schemas.exceptions import CastError
97-
from openapi_core.casting.schemas.factories import SchemaCastersFactory
98-
casters_factory = SchemaCastersFactory()
99-
caster = casters_factory.create(self)
100-
try:
101-
return caster(value)
102-
except (ValueError, TypeError):
103-
raise CastError(value, self.type)

openapi_core/validation/request/validators.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from itertools import chain
33
from six import iteritems
44

5+
from openapi_core.casting.schemas.exceptions import CastError
56
from openapi_core.schema.media_types.exceptions import (
67
InvalidMediaTypeValue, InvalidContentType,
78
)
@@ -112,10 +113,11 @@ def _get_parameters(self, request, params):
112113
deserialised = self._deserialise(param, raw_value)
113114
except OpenAPIParameterError as exc:
114115
errors.append(exc)
116+
continue
115117
else:
116118
try:
117119
casted = self._cast(param, deserialised)
118-
except OpenAPIParameterError as exc:
120+
except CastError as exc:
119121
errors.append(exc)
120122
continue
121123

@@ -153,7 +155,7 @@ def _get_body(self, request, operation):
153155
else:
154156
try:
155157
casted = self._cast(media_type, deserialised)
156-
except InvalidMediaTypeValue as exc:
158+
except CastError as exc:
157159
errors.append(exc)
158160
else:
159161
try:
@@ -167,7 +169,15 @@ def _deserialise(self, param_or_media_type, value):
167169
return param_or_media_type.deserialise(value)
168170

169171
def _cast(self, param_or_media_type, value):
170-
return param_or_media_type.cast(value)
172+
# return param_or_media_type.cast(value)
173+
if not param_or_media_type.schema:
174+
return value
175+
176+
from openapi_core.casting.schemas.exceptions import CastError
177+
from openapi_core.casting.schemas.factories import SchemaCastersFactory
178+
casters_factory = SchemaCastersFactory()
179+
caster = casters_factory.create(param_or_media_type.schema)
180+
return caster(value)
171181

172182
def _unmarshal(self, param_or_media_type, value):
173183
if not param_or_media_type.schema:

openapi_core/validation/response/validators.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""OpenAPI core validation response validators module"""
2+
from openapi_core.casting.schemas.exceptions import CastError
23
from openapi_core.schema.operations.exceptions import InvalidOperation
34
from openapi_core.schema.media_types.exceptions import (
45
InvalidMediaTypeValue, InvalidContentType,
@@ -87,7 +88,7 @@ def _get_data(self, response, operation_response):
8788
else:
8889
try:
8990
casted = self._cast(media_type, deserialised)
90-
except InvalidMediaTypeValue as exc:
91+
except CastError as exc:
9192
errors.append(exc)
9293
else:
9394
try:
@@ -109,7 +110,16 @@ def _deserialise(self, param_or_media_type, value):
109110
return param_or_media_type.deserialise(value)
110111

111112
def _cast(self, param_or_media_type, value):
112-
return param_or_media_type.cast(value)
113+
# return param_or_media_type.cast(value)
114+
if not param_or_media_type.schema:
115+
return value
116+
117+
from openapi_core.casting.schemas.exceptions import CastError
118+
from openapi_core.casting.schemas.factories import SchemaCastersFactory
119+
casters_factory = SchemaCastersFactory()
120+
caster = casters_factory.create(param_or_media_type.schema)
121+
return caster(value)
122+
113123

114124
def _unmarshal(self, param_or_media_type, value):
115125
if not param_or_media_type.schema:

tests/integration/contrib/flask/test_flask_decorator.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,13 @@ def test_endpoint_error(self, client):
100100
'errors': [
101101
{
102102
'class': (
103-
"<class 'openapi_core.schema.parameters."
104-
"exceptions.InvalidParameterValue'>"
103+
"<class 'openapi_core.casting.schemas.exceptions."
104+
"CastError'>"
105105
),
106106
'status': 400,
107107
'title': (
108-
'Invalid parameter value for `id`: '
109-
'Failed to cast value invalidparameter to type '
110-
'SchemaType.INTEGER'
108+
"Failed to cast value invalid literal for int() with "
109+
"base 10: 'invalidparameter' to type <class 'int'>"
111110
)
112111
}
113112
]

tests/integration/contrib/flask/test_flask_views.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ def test_endpoint_error(self, client):
8787
'errors': [
8888
{
8989
'class': (
90-
"<class 'openapi_core.schema.parameters."
91-
"exceptions.InvalidParameterValue'>"
90+
"<class 'openapi_core.casting.schemas.exceptions."
91+
"CastError'>"
9292
),
9393
'status': 400,
9494
'title': (
95-
'Invalid parameter value for `id`: '
96-
'Failed to cast value invalidparameter to type '
97-
'SchemaType.INTEGER'
95+
"Failed to cast value invalid literal for int() with "
96+
"base 10: 'invalidparameter' to type <class 'int'>"
9897
)
9998
}
10099
]

tests/integration/validation/test_petstore.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from uuid import UUID
66
from six import text_type
77

8+
from openapi_core.casting.schemas.exceptions import CastError
89
from openapi_core.extensions.models.models import BaseModel
910
from openapi_core.schema.media_types.exceptions import InvalidContentType
1011
from openapi_core.schema.parameters.exceptions import (
@@ -291,7 +292,7 @@ def test_get_pets_wrong_parameter_type(self, spec):
291292
path_pattern=path_pattern, args=query_params,
292293
)
293294

294-
with pytest.raises(InvalidParameterValue):
295+
with pytest.raises(CastError):
295296
validate_parameters(spec, request)
296297

297298
body = validate_body(spec, request)

tests/integration/validation/test_validators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import pytest
44
from six import text_type
55

6+
from openapi_core.casting.schemas.exceptions import CastError
67
from openapi_core.schema.media_types.exceptions import (
78
InvalidContentType, InvalidMediaTypeValue,
89
)
910
from openapi_core.extensions.models.models import BaseModel
1011
from openapi_core.schema.operations.exceptions import InvalidOperation
1112
from openapi_core.schema.parameters.exceptions import MissingRequiredParameter
12-
from openapi_core.schema.parameters.exceptions import InvalidParameterValue
1313
from openapi_core.schema.paths.exceptions import InvalidPath
1414
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
1515
from openapi_core.schema.responses.exceptions import (
@@ -317,7 +317,7 @@ def test_request_invalid_param(self, validator):
317317
result = validator.validate(request)
318318

319319
assert len(result.errors) == 1
320-
assert type(result.errors[0]) == InvalidParameterValue
320+
assert type(result.errors[0]) == CastError
321321
assert result.body is None
322322
assert result.parameters == RequestParameters()
323323

0 commit comments

Comments
 (0)