Skip to content

Commit 2d50e92

Browse files
committed
Schema enum
1 parent b777ac4 commit 2d50e92

File tree

4 files changed

+85
-16
lines changed

4 files changed

+85
-16
lines changed

openapi_core/enums.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,27 @@ class ParameterStyle(Enum):
2222
SPACE_DELIMITED = 'spaceDelimited'
2323
PIPE_DELIMITED = 'pipeDelimited'
2424
DEEP_OBJECT = 'deepObject'
25+
26+
27+
class SchemaType(Enum):
28+
29+
INTEGER = 'integer'
30+
NUMBER = 'number'
31+
STRING = 'string'
32+
BOOLEAN = 'boolean'
33+
ARRAY = 'array'
34+
OBJECT = 'object'
35+
36+
37+
class SchemaFormat(Enum):
38+
39+
NONE = None
40+
INT32 = 'int32'
41+
INT64 = 'int64'
42+
FLOAT = 'float'
43+
DOUBLE = 'double'
44+
BYTE = 'byte'
45+
BINARY = 'binary'
46+
DATE = 'date'
47+
DATETIME = 'date-time'
48+
PASSWORD = 'password'

openapi_core/schemas.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from json import loads
1010
from six import iteritems
1111

12+
from openapi_core.enums import SchemaType, SchemaFormat
1213
from openapi_core.exceptions import (
1314
InvalidValueType, UndefinedSchemaProperty, MissingProperty, InvalidValue,
1415
)
@@ -17,9 +18,9 @@
1718
log = logging.getLogger(__name__)
1819

1920
DEFAULT_CAST_CALLABLE_GETTER = {
20-
'integer': int,
21-
'number': float,
22-
'boolean': lambda x: bool(strtobool(x)),
21+
SchemaType.INTEGER: int,
22+
SchemaType.NUMBER: float,
23+
SchemaType.BOOLEAN: lambda x: bool(strtobool(x)),
2324
}
2425

2526

@@ -28,13 +29,13 @@ class Schema(object):
2829

2930
def __init__(
3031
self, schema_type, model=None, properties=None, items=None,
31-
spec_format=None, required=None, default=None, nullable=False,
32+
schema_format=None, required=None, default=None, nullable=False,
3233
enum=None, deprecated=False, all_of=None):
33-
self.type = schema_type
34+
self.type = SchemaType(schema_type)
3435
self.model = model
3536
self.properties = properties and dict(properties) or {}
3637
self.items = items
37-
self.format = spec_format
38+
self.format = SchemaFormat(schema_format)
3839
self.required = required or []
3940
self.default = default
4041
self.nullable = nullable
@@ -57,8 +58,8 @@ def get_all_properties(self):
5758
def get_cast_mapping(self):
5859
mapping = DEFAULT_CAST_CALLABLE_GETTER.copy()
5960
mapping.update({
60-
'array': self._unmarshal_collection,
61-
'object': self._unmarshal_object,
61+
SchemaType.ARRAY: self._unmarshal_collection,
62+
SchemaType.OBJECT: self._unmarshal_object,
6263
})
6364

6465
return defaultdict(lambda: lambda x: x, mapping)
@@ -159,6 +160,7 @@ def create(self, schema_spec):
159160
schema_deref = self.dereferencer.dereference(schema_spec)
160161

161162
schema_type = schema_deref['type']
163+
schema_format = schema_deref.get('format')
162164
model = schema_deref.get('x-model', None)
163165
required = schema_deref.get('required', False)
164166
default = schema_deref.get('default', None)
@@ -183,8 +185,8 @@ def create(self, schema_spec):
183185

184186
return Schema(
185187
schema_type, model=model, properties=properties, items=items,
186-
required=required, default=default, nullable=nullable, enum=enum,
187-
deprecated=deprecated, all_of=all_of,
188+
schema_format=schema_format, required=required, default=default,
189+
nullable=nullable, enum=enum, deprecated=deprecated, all_of=all_of,
188190
)
189191

190192
@property

tests/integration/data/v3.0/petstore.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ components:
119119
Tag:
120120
type: string
121121
enum:
122-
- Cat
123-
- Dog
124-
- Bird
122+
- cats
123+
- dogs
124+
- birds
125125
Position:
126126
type: integer
127127
enum:
@@ -148,7 +148,7 @@ components:
148148
name:
149149
type: string
150150
tag:
151-
type: "#/components/schemas/Tag"
151+
$ref: "#/components/schemas/Tag"
152152
address:
153153
$ref: "#/components/schemas/Address"
154154
position:

tests/integration/test_petstore.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ def test_spec(self, spec, spec_dict):
128128
continue
129129

130130
assert type(parameter.schema) == Schema
131-
assert parameter.schema.type == schema_spec['type']
131+
assert parameter.schema.type.value ==\
132+
schema_spec['type']
133+
assert parameter.schema.format.value ==\
134+
schema_spec.get('format')
132135
assert parameter.schema.required == schema_spec.get(
133136
'required', [])
134137

@@ -160,7 +163,10 @@ def test_spec(self, spec, spec_dict):
160163
continue
161164

162165
assert type(media_type.schema) == Schema
163-
assert media_type.schema.type == schema_spec['type']
166+
assert media_type.schema.type.value ==\
167+
schema_spec['type']
168+
assert media_type.schema.format.value ==\
169+
schema_spec.get('format')
164170
assert media_type.schema.required == schema_spec.get(
165171
'required', False)
166172

@@ -207,6 +213,43 @@ def test_get_pets(self, spec, response_validator):
207213
assert response_result.errors == []
208214
assert response_result.data == data_json
209215

216+
def test_get_pets_tag_param(self, spec, response_validator):
217+
host_url = 'http://petstore.swagger.io/v1'
218+
path_pattern = '/v1/pets'
219+
query_params = {
220+
'limit': '20',
221+
'ids': ['12', '13'],
222+
}
223+
224+
request = MockRequest(
225+
host_url, 'GET', '/pets',
226+
path_pattern=path_pattern, args=query_params,
227+
)
228+
229+
parameters = request.get_parameters(spec)
230+
body = request.get_body(spec)
231+
232+
assert parameters == {
233+
'query': {
234+
'limit': 20,
235+
'page': 1,
236+
'search': '',
237+
'ids': [12, 13],
238+
}
239+
}
240+
assert body is None
241+
242+
data_json = {
243+
'data': [],
244+
}
245+
data = json.dumps(data_json)
246+
response = MockResponse(data)
247+
248+
response_result = response_validator.validate(request, response)
249+
250+
assert response_result.errors == []
251+
assert response_result.data == data_json
252+
210253
def test_get_pets_wrong_parameter_type(self, spec):
211254
host_url = 'http://petstore.swagger.io/v1'
212255
path_pattern = '/v1/pets'

0 commit comments

Comments
 (0)