Skip to content

Commit 69a580a

Browse files
committed
The integer_types is always a tuple. When checking
if an instance is a number it fails because it's doing a comparison against a tuple instead of real type. ➜ python -c "from six import integer_types;import sys;print(integer_types);print(sys.version)" (<type 'int'>, <type 'long'>) 2.7.16 (default, Apr 6 2019, 01:42:57) [GCC 8.3.0] ➜ python3 -c "from six import integer_types;import sys;print(integer_types);print(sys.version)" (<class 'int'>,) 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0] And spec defines a number as both int and float https://swagger.io/docs/specification/data-models/data-types/#numbers so both validators need to support both types.
1 parent f274836 commit 69a580a

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Schema(object):
5454
SchemaType.ANY: lambda x: True,
5555
SchemaType.BOOLEAN: TypeValidator(bool),
5656
SchemaType.INTEGER: TypeValidator(integer_types, exclude=bool),
57-
SchemaType.NUMBER: TypeValidator(integer_types, float, exclude=bool),
57+
SchemaType.NUMBER: TypeValidator(integer_types + (float, ), exclude=bool),
5858
SchemaType.STRING: TypeValidator(
5959
text_type, date, datetime, binary_type, UUID),
6060
SchemaType.ARRAY: TypeValidator(list, tuple),
@@ -228,16 +228,16 @@ def _unmarshal_string(self, value, custom_formatters=None, strict=True):
228228
"Failed to format value {value} to format {type}: {exception}", value, self.format, exc)
229229

230230
def _unmarshal_integer(self, value, custom_formatters=None, strict=True):
231-
if strict and not isinstance(value, (integer_types, )):
231+
if strict and not isinstance(value, integer_types):
232232
raise InvalidSchemaValue("Value {value} is not of type {type}", value, self.type)
233233

234234
return int(value)
235235

236236
def _unmarshal_number(self, value, custom_formatters=None, strict=True):
237-
if strict and not isinstance(value, (float, )):
237+
if strict and not isinstance(value, (float, ) + integer_types):
238238
raise InvalidSchemaValue("Value {value} is not of type {type}", value, self.type)
239239

240-
return float(value)
240+
return value
241241

242242
def _unmarshal_boolean(self, value, custom_formatters=None, strict=True):
243243
if strict and not isinstance(value, (bool, )):

tests/unit/schema/test_schemas.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ def test_number_string_invalid(self):
262262
with pytest.raises(InvalidSchemaValue):
263263
schema.unmarshal(value)
264264

265-
def test_number_int_invalid(self):
265+
def test_number_int(self):
266266
schema = Schema('number')
267267
value = 1
268+
result = schema.unmarshal(value)
268269

269-
with pytest.raises(InvalidSchemaValue):
270-
schema.unmarshal(value)
270+
assert result == value
271271

272272

273273
class TestSchemaValidate(object):

0 commit comments

Comments
 (0)