Parameter enums

This commit is contained in:
Artur Maciag 2017-11-14 11:28:45 +00:00
parent 56f6a6db7d
commit b777ac4bfe
4 changed files with 86 additions and 11 deletions

24
openapi_core/enums.py Normal file
View file

@ -0,0 +1,24 @@
from enum import Enum
class ParameterLocation(Enum):
PATH = 'path'
QUERY = 'query'
HEADER = 'header'
COOKIE = 'cookie'
@classmethod
def has_value(cls, value):
return (any(value == item.value for item in cls))
class ParameterStyle(Enum):
MATRIX = 'matrix'
LABEL = 'label'
FORM = 'form'
SIMPLE = 'simple'
SPACE_DELIMITED = 'spaceDelimited'
PIPE_DELIMITED = 'pipeDelimited'
DEEP_OBJECT = 'deepObject'

View file

@ -4,6 +4,7 @@ import warnings
from six import iteritems
from openapi_core.enums import ParameterLocation, ParameterStyle
from openapi_core.exceptions import (
EmptyValue, InvalidValueType, InvalidParameterValue,
)
@ -17,17 +18,32 @@ class Parameter(object):
def __init__(
self, name, location, schema=None, required=False,
deprecated=False, allow_empty_value=False,
items=None, collection_format=None):
items=None, style=None, explode=None):
self.name = name
self.location = location
self.location = ParameterLocation(location)
self.schema = schema
self.required = True if self.location == "path" else required
self.required = (
True if self.location == ParameterLocation.PATH else required
)
self.deprecated = deprecated
self.allow_empty_value = (
allow_empty_value if self.location == "query" else False
allow_empty_value if self.location == ParameterLocation.QUERY
else False
)
self.items = items
self.collection_format = collection_format
self.style = ParameterStyle(style or self.default_style)
self.explode = explode or self.default_explode
@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
def unmarshal(self, value):
if self.deprecated:
@ -36,7 +52,7 @@ class Parameter(object):
DeprecationWarning,
)
if (self.location == "query" and value == "" and
if (self.location == ParameterLocation.QUERY and value == "" and
not self.allow_empty_value):
raise EmptyValue(
"Value of {0} parameter cannot be empty".format(self.name))
@ -89,6 +105,9 @@ class ParametersGenerator(object):
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:
@ -100,5 +119,6 @@ class ParametersGenerator(object):
parameter_name, parameter_in,
schema=schema, required=required,
allow_empty_value=allow_empty_value,
style=style, explode=explode,
),
)

View file

@ -1,6 +1,7 @@
"""OpenAPI core validators module"""
from six import iteritems
from openapi_core.enums import ParameterLocation
from openapi_core.exceptions import (
OpenAPIMappingError, MissingParameter, MissingBody, InvalidResponse,
)
@ -8,8 +9,6 @@ from openapi_core.exceptions import (
class RequestParameters(dict):
valid_locations = ['path', 'query', 'headers', 'cookies']
def __getitem__(self, location):
self.validate_location(location)
@ -20,7 +19,7 @@ class RequestParameters(dict):
@classmethod
def validate_location(cls, location):
if location not in cls.valid_locations:
if not ParameterLocation.has_value(location):
raise OpenAPIMappingError(
"Unknown parameter location: {0}".format(str(location)))
@ -95,7 +94,7 @@ class RequestValidator(object):
except OpenAPIMappingError as exc:
errors.append(exc)
else:
parameters[param.location][param_name] = value
parameters[param.location.value][param_name] = value
if operation.request_body is not None:
try:
@ -118,7 +117,7 @@ class RequestValidator(object):
def _get_raw_value(self, request, param):
try:
return request.parameters[param.location][param.name]
return request.parameters[param.location.value][param.name]
except KeyError:
raise MissingParameter(
"Missing required `{0}` parameter".format(param.name))

View file

@ -1,9 +1,41 @@
import pytest
from openapi_core.enums import ParameterStyle
from openapi_core.exceptions import EmptyValue
from openapi_core.parameters import Parameter
class TestParameterInit(object):
def test_path(self):
param = Parameter('param', 'path')
assert param.allow_empty_value is False
assert param.style == ParameterStyle.SIMPLE
assert param.explode is False
def test_query(self):
param = Parameter('param', 'query')
assert param.allow_empty_value is False
assert param.style == ParameterStyle.FORM
assert param.explode is True
def test_header(self):
param = Parameter('param', 'header')
assert param.allow_empty_value is False
assert param.style == ParameterStyle.SIMPLE
assert param.explode is False
def test_cookie(self):
param = Parameter('param', 'cookie')
assert param.allow_empty_value is False
assert param.style == ParameterStyle.FORM
assert param.explode is True
class TestParameterUnmarshal(object):
def test_deprecated(self):