Skip to content

Commit c087ea6

Browse files
committed
Fix number validator
The `integer_types` is alaways a tuple. When checking if instance is number or tuple it fails becase it's doing comparison against 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 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 c087ea6

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
@@ -53,8 +53,8 @@ class Schema(object):
5353
TYPE_VALIDATOR_CALLABLE_GETTER = {
5454
SchemaType.ANY: lambda x: True,
5555
SchemaType.BOOLEAN: TypeValidator(bool),
56-
SchemaType.INTEGER: TypeValidator(integer_types, exclude=bool),
57-
SchemaType.NUMBER: TypeValidator(integer_types, float, exclude=bool),
56+
SchemaType.INTEGER: TypeValidator(*integer_types, 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),
@@ -234,10 +234,10 @@ def _unmarshal_integer(self, value, custom_formatters=None, strict=True):
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)