2017-09-21 11:51:37 +00:00
|
|
|
"""OpenAPI core parameters module"""
|
|
|
|
import logging
|
2017-10-18 13:42:10 +00:00
|
|
|
import warnings
|
|
|
|
|
2017-11-14 16:05:03 +00:00
|
|
|
from functools import lru_cache
|
2017-11-06 11:00:13 +00:00
|
|
|
from six import iteritems
|
|
|
|
|
2017-11-14 16:05:03 +00:00
|
|
|
from openapi_core.enums import ParameterLocation, ParameterStyle, SchemaType
|
2017-11-03 11:18:48 +00:00
|
|
|
from openapi_core.exceptions import (
|
|
|
|
EmptyValue, InvalidValueType, InvalidParameterValue,
|
|
|
|
)
|
2017-09-21 11:51:37 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2017-11-14 16:05:03 +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
|
|
|
|
|
|
|
class Parameter(object):
|
|
|
|
"""Represents an OpenAPI operation Parameter."""
|
|
|
|
|
|
|
|
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):
|
|
|
|
return PARAMETER_STYLE_DESERIALIZERS[self.style]
|
|
|
|
|
|
|
|
def deserialize(self, value):
|
|
|
|
if not self.aslist or self.explode:
|
|
|
|
return value
|
|
|
|
|
|
|
|
deserializer = self.get_dererializer()
|
|
|
|
return deserializer(value)
|
|
|
|
|
2017-09-21 11:51:37 +00:00
|
|
|
def unmarshal(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):
|
|
|
|
raise EmptyValue(
|
2017-11-03 11:18:48 +00:00
|
|
|
"Value of {0} parameter cannot be empty".format(self.name))
|
2017-10-18 13:42:10 +00:00
|
|
|
|
2017-09-21 11:51:37 +00:00
|
|
|
if not self.schema:
|
|
|
|
return value
|
|
|
|
|
2017-11-14 16:05:03 +00:00
|
|
|
deserialized = self.deserialize(value)
|
|
|
|
|
2017-11-03 11:18:48 +00:00
|
|
|
try:
|
2017-11-14 16:05:03 +00:00
|
|
|
return self.schema.unmarshal(deserialized)
|
2017-11-03 11:18:48 +00:00
|
|
|
except InvalidValueType as exc:
|
|
|
|
raise InvalidParameterValue(str(exc))
|
2017-09-21 11:51:37 +00:00
|
|
|
|
|
|
|
|
2017-11-14 16:05:03 +00:00
|
|
|
class ParameterFactory(object):
|
|
|
|
|
|
|
|
def __init__(self, dereferencer, schemas_registry):
|
|
|
|
self.dereferencer = dereferencer
|
|
|
|
self.schemas_registry = schemas_registry
|
|
|
|
|
|
|
|
def create(self, parameter_spec, parameter_name=None):
|
|
|
|
parameter_deref = self.dereferencer.dereference(parameter_spec)
|
|
|
|
|
|
|
|
parameter_name = parameter_name or parameter_deref['name']
|
|
|
|
parameter_in = parameter_deref.get('in', 'header')
|
|
|
|
|
|
|
|
allow_empty_value = parameter_deref.get('allowEmptyValue')
|
|
|
|
required = parameter_deref.get('required', False)
|
|
|
|
|
|
|
|
style = parameter_deref.get('style')
|
|
|
|
explode = parameter_deref.get('explode')
|
|
|
|
|
|
|
|
schema_spec = parameter_deref.get('schema', None)
|
|
|
|
schema = None
|
|
|
|
if schema_spec:
|
|
|
|
schema, _ = self.schemas_registry.get_or_create(schema_spec)
|
|
|
|
|
|
|
|
return Parameter(
|
|
|
|
parameter_name, parameter_in,
|
|
|
|
schema=schema, required=required,
|
|
|
|
allow_empty_value=allow_empty_value,
|
|
|
|
style=style, explode=explode,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-09-21 11:51:37 +00:00
|
|
|
class ParametersGenerator(object):
|
|
|
|
|
2017-09-22 08:54:37 +00:00
|
|
|
def __init__(self, dereferencer, schemas_registry):
|
2017-09-21 11:51:37 +00:00
|
|
|
self.dereferencer = dereferencer
|
2017-09-22 08:54:37 +00:00
|
|
|
self.schemas_registry = schemas_registry
|
2017-09-21 11:51:37 +00:00
|
|
|
|
2017-11-06 11:00:13 +00:00
|
|
|
def generate(self, parameters):
|
2017-11-14 16:05:03 +00:00
|
|
|
for parameter_name, parameter_spec in iteritems(parameters):
|
|
|
|
parameter = self.parameter_factory.create(
|
|
|
|
parameter_spec, parameter_name=parameter_name)
|
|
|
|
|
|
|
|
yield (parameter_name, parameter)
|
2017-11-06 11:00:13 +00:00
|
|
|
|
|
|
|
def generate_from_list(self, parameters_list):
|
2017-11-14 16:05:03 +00:00
|
|
|
for parameter_spec in parameters_list:
|
|
|
|
parameter = self.parameter_factory.create(parameter_spec)
|
|
|
|
|
|
|
|
yield (parameter.name, parameter)
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lru_cache()
|
|
|
|
def parameter_factory(self):
|
|
|
|
return ParameterFactory(self.dereferencer, self.schemas_registry)
|