Skip to content

Commit b777ac4

Browse files
committed
Parameter enums
1 parent 56f6a6d commit b777ac4

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

openapi_core/enums.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from enum import Enum
2+
3+
4+
class ParameterLocation(Enum):
5+
6+
PATH = 'path'
7+
QUERY = 'query'
8+
HEADER = 'header'
9+
COOKIE = 'cookie'
10+
11+
@classmethod
12+
def has_value(cls, value):
13+
return (any(value == item.value for item in cls))
14+
15+
16+
class ParameterStyle(Enum):
17+
18+
MATRIX = 'matrix'
19+
LABEL = 'label'
20+
FORM = 'form'
21+
SIMPLE = 'simple'
22+
SPACE_DELIMITED = 'spaceDelimited'
23+
PIPE_DELIMITED = 'pipeDelimited'
24+
DEEP_OBJECT = 'deepObject'

openapi_core/parameters.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from six import iteritems
66

7+
from openapi_core.enums import ParameterLocation, ParameterStyle
78
from openapi_core.exceptions import (
89
EmptyValue, InvalidValueType, InvalidParameterValue,
910
)
@@ -17,17 +18,32 @@ class Parameter(object):
1718
def __init__(
1819
self, name, location, schema=None, required=False,
1920
deprecated=False, allow_empty_value=False,
20-
items=None, collection_format=None):
21+
items=None, style=None, explode=None):
2122
self.name = name
22-
self.location = location
23+
self.location = ParameterLocation(location)
2324
self.schema = schema
24-
self.required = True if self.location == "path" else required
25+
self.required = (
26+
True if self.location == ParameterLocation.PATH else required
27+
)
2528
self.deprecated = deprecated
2629
self.allow_empty_value = (
27-
allow_empty_value if self.location == "query" else False
30+
allow_empty_value if self.location == ParameterLocation.QUERY
31+
else False
2832
)
2933
self.items = items
30-
self.collection_format = collection_format
34+
self.style = ParameterStyle(style or self.default_style)
35+
self.explode = explode or self.default_explode
36+
37+
@property
38+
def default_style(self):
39+
simple_locations = [ParameterLocation.PATH, ParameterLocation.HEADER]
40+
return (
41+
'simple' if self.location in simple_locations else "form"
42+
)
43+
44+
@property
45+
def default_explode(self):
46+
return self.style == ParameterStyle.FORM
3147

3248
def unmarshal(self, value):
3349
if self.deprecated:
@@ -36,7 +52,7 @@ def unmarshal(self, value):
3652
DeprecationWarning,
3753
)
3854

39-
if (self.location == "query" and value == "" and
55+
if (self.location == ParameterLocation.QUERY and value == "" and
4056
not self.allow_empty_value):
4157
raise EmptyValue(
4258
"Value of {0} parameter cannot be empty".format(self.name))
@@ -89,6 +105,9 @@ def generate_from_list(self, parameters_list):
89105
allow_empty_value = parameter_deref.get('allowEmptyValue')
90106
required = parameter_deref.get('required', False)
91107

108+
style = parameter_deref.get('style')
109+
explode = parameter_deref.get('explode')
110+
92111
schema_spec = parameter_deref.get('schema', None)
93112
schema = None
94113
if schema_spec:
@@ -100,5 +119,6 @@ def generate_from_list(self, parameters_list):
100119
parameter_name, parameter_in,
101120
schema=schema, required=required,
102121
allow_empty_value=allow_empty_value,
122+
style=style, explode=explode,
103123
),
104124
)

openapi_core/validators.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"""OpenAPI core validators module"""
22
from six import iteritems
33

4+
from openapi_core.enums import ParameterLocation
45
from openapi_core.exceptions import (
56
OpenAPIMappingError, MissingParameter, MissingBody, InvalidResponse,
67
)
78

89

910
class RequestParameters(dict):
1011

11-
valid_locations = ['path', 'query', 'headers', 'cookies']
12-
1312
def __getitem__(self, location):
1413
self.validate_location(location)
1514

@@ -20,7 +19,7 @@ def __setitem__(self, location, value):
2019

2120
@classmethod
2221
def validate_location(cls, location):
23-
if location not in cls.valid_locations:
22+
if not ParameterLocation.has_value(location):
2423
raise OpenAPIMappingError(
2524
"Unknown parameter location: {0}".format(str(location)))
2625

@@ -95,7 +94,7 @@ def validate(self, request):
9594
except OpenAPIMappingError as exc:
9695
errors.append(exc)
9796
else:
98-
parameters[param.location][param_name] = value
97+
parameters[param.location.value][param_name] = value
9998

10099
if operation.request_body is not None:
101100
try:
@@ -118,7 +117,7 @@ def validate(self, request):
118117

119118
def _get_raw_value(self, request, param):
120119
try:
121-
return request.parameters[param.location][param.name]
120+
return request.parameters[param.location.value][param.name]
122121
except KeyError:
123122
raise MissingParameter(
124123
"Missing required `{0}` parameter".format(param.name))

tests/unit/test_paramters.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
import pytest
22

3+
from openapi_core.enums import ParameterStyle
34
from openapi_core.exceptions import EmptyValue
45
from openapi_core.parameters import Parameter
56

67

8+
class TestParameterInit(object):
9+
10+
def test_path(self):
11+
param = Parameter('param', 'path')
12+
13+
assert param.allow_empty_value is False
14+
assert param.style == ParameterStyle.SIMPLE
15+
assert param.explode is False
16+
17+
def test_query(self):
18+
param = Parameter('param', 'query')
19+
20+
assert param.allow_empty_value is False
21+
assert param.style == ParameterStyle.FORM
22+
assert param.explode is True
23+
24+
def test_header(self):
25+
param = Parameter('param', 'header')
26+
27+
assert param.allow_empty_value is False
28+
assert param.style == ParameterStyle.SIMPLE
29+
assert param.explode is False
30+
31+
def test_cookie(self):
32+
param = Parameter('param', 'cookie')
33+
34+
assert param.allow_empty_value is False
35+
assert param.style == ParameterStyle.FORM
36+
assert param.explode is True
37+
38+
739
class TestParameterUnmarshal(object):
840

941
def test_deprecated(self):

0 commit comments

Comments
 (0)