2018-04-17 12:18:40 +00:00
|
|
|
"""OpenAPI core parameters models module"""
|
2017-09-21 11:51:37 +00:00
|
|
|
import logging
|
2017-10-18 13:42:10 +00:00
|
|
|
import warnings
|
|
|
|
|
2018-04-18 10:49:49 +00:00
|
|
|
from openapi_core.schema.parameters.enums import (
|
|
|
|
ParameterLocation, ParameterStyle,
|
|
|
|
)
|
2018-04-18 10:39:03 +00:00
|
|
|
from openapi_core.schema.parameters.exceptions import (
|
|
|
|
MissingRequiredParameter, MissingParameter, InvalidParameterValue,
|
|
|
|
EmptyParameterValue,
|
|
|
|
)
|
2018-04-17 12:18:40 +00:00
|
|
|
from openapi_core.schema.schemas.enums import SchemaType
|
2019-10-20 13:38:41 +00:00
|
|
|
from openapi_core.schema.schemas.exceptions import (
|
2020-01-23 22:48:34 +00:00
|
|
|
ValidateError,
|
2019-10-20 13:38:41 +00:00
|
|
|
)
|
2020-01-23 22:48:34 +00:00
|
|
|
from openapi_core.casting.schemas.exceptions import CastError
|
2019-10-25 23:34:42 +00:00
|
|
|
from openapi_core.unmarshalling.schemas.exceptions import UnmarshalError
|
2017-09-21 11:51:37 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Parameter(object):
|
|
|
|
"""Represents an OpenAPI operation Parameter."""
|
|
|
|
|
2018-04-17 12:18:40 +00:00
|
|
|
PARAMETER_STYLE_DESERIALIZERS = {
|
|
|
|
ParameterStyle.FORM: lambda x: x.split(','),
|
|
|
|
ParameterStyle.SIMPLE: lambda x: x.split(','),
|
|
|
|
ParameterStyle.SPACE_DELIMITED: lambda x: x.split(' '),
|
|
|
|
ParameterStyle.PIPE_DELIMITED: lambda x: x.split('|'),
|
|
|
|
}
|
|
|
|
|
2017-09-21 11:51:37 +00:00
|
|
|
def __init__(
|
2017-10-18 13:42:10 +00:00
|
|
|
self, name, location, schema=None, required=False,
|
|
|
|
deprecated=False, allow_empty_value=False,
|
2017-11-14 11:28:45 +00:00
|
|
|
items=None, style=None, explode=None):
|
2017-09-21 11:51:37 +00:00
|
|
|
self.name = name
|
2017-11-14 11:28:45 +00:00
|
|
|
self.location = ParameterLocation(location)
|
2017-09-21 11:51:37 +00:00
|
|
|
self.schema = schema
|
2017-11-14 11:28:45 +00:00
|
|
|
self.required = (
|
|
|
|
True if self.location == ParameterLocation.PATH else required
|
|
|
|
)
|
2017-09-21 11:51:37 +00:00
|
|
|
self.deprecated = deprecated
|
2017-10-18 13:42:10 +00:00
|
|
|
self.allow_empty_value = (
|
2017-11-14 11:28:45 +00:00
|
|
|
allow_empty_value if self.location == ParameterLocation.QUERY
|
|
|
|
else False
|
2017-10-18 13:42:10 +00:00
|
|
|
)
|
2017-09-21 11:51:37 +00:00
|
|
|
self.items = items
|
2017-11-14 11:28:45 +00:00
|
|
|
self.style = ParameterStyle(style or self.default_style)
|
2017-11-14 16:05:03 +00:00
|
|
|
self.explode = self.default_explode if explode is None else explode
|
|
|
|
|
|
|
|
@property
|
|
|
|
def aslist(self):
|
|
|
|
return (
|
|
|
|
self.schema and
|
|
|
|
self.schema.type in [SchemaType.ARRAY, SchemaType.OBJECT]
|
|
|
|
)
|
2017-11-14 11:28:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def default_style(self):
|
|
|
|
simple_locations = [ParameterLocation.PATH, ParameterLocation.HEADER]
|
|
|
|
return (
|
|
|
|
'simple' if self.location in simple_locations else "form"
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_explode(self):
|
|
|
|
return self.style == ParameterStyle.FORM
|
2017-09-21 11:51:37 +00:00
|
|
|
|
2017-11-14 16:05:03 +00:00
|
|
|
def get_dererializer(self):
|
2018-04-17 12:18:40 +00:00
|
|
|
return self.PARAMETER_STYLE_DESERIALIZERS[self.style]
|
2017-11-14 16:05:03 +00:00
|
|
|
|
|
|
|
def deserialize(self, value):
|
|
|
|
if not self.aslist or self.explode:
|
|
|
|
return value
|
|
|
|
|
|
|
|
deserializer = self.get_dererializer()
|
|
|
|
return deserializer(value)
|
|
|
|
|
2019-09-03 00:38:19 +00:00
|
|
|
def get_raw_value(self, request):
|
2018-04-18 10:39:03 +00:00
|
|
|
location = request.parameters[self.location.value]
|
|
|
|
|
2018-08-17 13:29:41 +00:00
|
|
|
if self.name not in location:
|
2018-04-18 10:39:03 +00:00
|
|
|
if self.required:
|
2018-09-06 14:50:16 +00:00
|
|
|
raise MissingRequiredParameter(self.name)
|
2018-04-18 10:39:03 +00:00
|
|
|
|
2019-10-25 23:34:42 +00:00
|
|
|
raise MissingParameter(self.name)
|
2018-04-18 10:39:03 +00:00
|
|
|
|
|
|
|
if self.aslist and self.explode:
|
2019-11-19 04:07:20 +00:00
|
|
|
if hasattr(location, 'getall'):
|
|
|
|
return location.getall(self.name)
|
2018-04-18 10:39:03 +00:00
|
|
|
return location.getlist(self.name)
|
|
|
|
|
2018-08-17 13:29:41 +00:00
|
|
|
return location[self.name]
|
2018-04-18 10:39:03 +00:00
|
|
|
|
2019-09-03 00:38:19 +00:00
|
|
|
def cast(self, value):
|
2017-10-18 13:42:10 +00:00
|
|
|
if self.deprecated:
|
|
|
|
warnings.warn(
|
|
|
|
"{0} parameter is deprecated".format(self.name),
|
|
|
|
DeprecationWarning,
|
|
|
|
)
|
|
|
|
|
2017-11-14 11:28:45 +00:00
|
|
|
if (self.location == ParameterLocation.QUERY and value == "" and
|
2017-10-18 13:42:10 +00:00
|
|
|
not self.allow_empty_value):
|
2018-09-06 14:50:16 +00:00
|
|
|
raise EmptyParameterValue(self.name)
|
2017-10-18 13:42:10 +00:00
|
|
|
|
2017-09-21 11:51:37 +00:00
|
|
|
if not self.schema:
|
|
|
|
return value
|
|
|
|
|
2018-08-17 10:42:52 +00:00
|
|
|
try:
|
|
|
|
deserialized = self.deserialize(value)
|
|
|
|
except (ValueError, AttributeError) as exc:
|
2018-09-06 14:50:16 +00:00
|
|
|
raise InvalidParameterValue(self.name, exc)
|
2017-11-14 16:05:03 +00:00
|
|
|
|
2019-09-02 22:14:37 +00:00
|
|
|
try:
|
2019-09-03 00:38:19 +00:00
|
|
|
return self.schema.cast(deserialized)
|
2019-10-20 13:38:41 +00:00
|
|
|
except CastError as exc:
|
2019-09-03 00:38:19 +00:00
|
|
|
raise InvalidParameterValue(self.name, exc)
|
|
|
|
|
|
|
|
def unmarshal(self, value, custom_formatters=None, resolver=None):
|
|
|
|
if not self.schema:
|
|
|
|
return value
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.schema.validate(value, resolver=resolver)
|
2019-10-20 13:38:41 +00:00
|
|
|
except ValidateError as exc:
|
2019-09-02 22:14:37 +00:00
|
|
|
raise InvalidParameterValue(self.name, exc)
|
|
|
|
|
2017-11-03 11:18:48 +00:00
|
|
|
try:
|
2019-09-11 21:47:16 +00:00
|
|
|
return self.schema.unmarshal(
|
2019-09-03 00:38:19 +00:00
|
|
|
value,
|
2019-03-22 01:51:47 +00:00
|
|
|
custom_formatters=custom_formatters,
|
2019-09-02 22:14:37 +00:00
|
|
|
strict=True,
|
2019-03-22 01:51:47 +00:00
|
|
|
)
|
2019-10-20 13:38:41 +00:00
|
|
|
except UnmarshalError as exc:
|
2018-09-06 14:50:16 +00:00
|
|
|
raise InvalidParameterValue(self.name, exc)
|