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-06 11:00:13 +00:00
|
|
|
from six import iteritems
|
|
|
|
|
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__)
|
|
|
|
|
|
|
|
|
|
|
|
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-09-21 11:51:37 +00:00
|
|
|
items=None, collection_format=None):
|
|
|
|
self.name = name
|
|
|
|
self.location = location
|
|
|
|
self.schema = schema
|
2017-10-18 13:42:10 +00:00
|
|
|
self.required = True if self.location == "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 = (
|
|
|
|
allow_empty_value if self.location == "query" else False
|
|
|
|
)
|
2017-09-21 11:51:37 +00:00
|
|
|
self.items = items
|
|
|
|
self.collection_format = collection_format
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
if (self.location == "query" and value == "" and
|
|
|
|
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-03 11:18:48 +00:00
|
|
|
try:
|
|
|
|
return self.schema.unmarshal(value)
|
|
|
|
except InvalidValueType as exc:
|
|
|
|
raise InvalidParameterValue(str(exc))
|
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):
|
|
|
|
for parameter_name, parameter in iteritems(parameters):
|
|
|
|
parameter_deref = self.dereferencer.dereference(parameter)
|
|
|
|
|
|
|
|
parameter_in = parameter_deref.get('in', 'header')
|
|
|
|
|
|
|
|
allow_empty_value = parameter_deref.get('allowEmptyValue')
|
|
|
|
required = parameter_deref.get('required', False)
|
|
|
|
|
|
|
|
schema_spec = parameter_deref.get('schema', None)
|
|
|
|
schema = None
|
|
|
|
if schema_spec:
|
|
|
|
schema, _ = self.schemas_registry.get_or_create(schema_spec)
|
|
|
|
|
|
|
|
yield (
|
|
|
|
parameter_name,
|
|
|
|
Parameter(
|
|
|
|
parameter_name, parameter_in,
|
|
|
|
schema=schema, required=required,
|
|
|
|
allow_empty_value=allow_empty_value,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def generate_from_list(self, parameters_list):
|
|
|
|
for parameter in parameters_list:
|
2017-09-21 11:51:37 +00:00
|
|
|
parameter_deref = self.dereferencer.dereference(parameter)
|
|
|
|
|
2017-11-06 11:00:13 +00:00
|
|
|
parameter_name = parameter_deref['name']
|
|
|
|
parameter_in = parameter_deref.get('in', 'header')
|
|
|
|
|
2017-10-18 13:42:10 +00:00
|
|
|
allow_empty_value = parameter_deref.get('allowEmptyValue')
|
2017-09-21 11:51:37 +00:00
|
|
|
required = parameter_deref.get('required', False)
|
|
|
|
|
|
|
|
schema_spec = parameter_deref.get('schema', None)
|
|
|
|
schema = None
|
|
|
|
if schema_spec:
|
2017-09-22 08:54:37 +00:00
|
|
|
schema, _ = self.schemas_registry.get_or_create(schema_spec)
|
2017-09-21 11:51:37 +00:00
|
|
|
|
|
|
|
yield (
|
2017-11-06 11:00:13 +00:00
|
|
|
parameter_name,
|
2017-09-21 11:51:37 +00:00
|
|
|
Parameter(
|
2017-11-06 11:00:13 +00:00
|
|
|
parameter_name, parameter_in,
|
2017-10-18 13:42:10 +00:00
|
|
|
schema=schema, required=required,
|
|
|
|
allow_empty_value=allow_empty_value,
|
2017-09-21 11:51:37 +00:00
|
|
|
),
|
|
|
|
)
|