diff --git a/openapi_schema_validator/_format.py b/openapi_schema_validator/_format.py index a4544d1..fa37c68 100644 --- a/openapi_schema_validator/_format.py +++ b/openapi_schema_validator/_format.py @@ -12,7 +12,8 @@ def is_int32(instance: object) -> bool: # bool inherits from int, so ensure bools aren't reported as ints if isinstance(instance, bool): return True - if not isinstance(instance, int): + # floats are range-checked too: 1e10 is an integer in OAS 3.1+ + if not isinstance(instance, (int, float)): return True return ~(1 << 31) < instance < 1 << 31 @@ -21,7 +22,8 @@ def is_int64(instance: object) -> bool: # bool inherits from int, so ensure bools aren't reported as ints if isinstance(instance, bool): return True - if not isinstance(instance, int): + # floats are range-checked too: 1e19 is an integer in OAS 3.1+ + if not isinstance(instance, (int, float)): return True return ~(1 << 63) < instance < 1 << 63 diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py index df4e5a6..784f5a7 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/test_validators.py @@ -98,6 +98,25 @@ def test_formats_ignored( assert result is None + @pytest.mark.parametrize( + "format,value", + [ + ("int32", 2147483648.0), + ("int32", -2147483649.0), + ("int32", 1e10), + ("int64", 9223372036854775808.0), + ("int64", 1e19), + ], + ) + def test_integer_formats_out_of_range_float_invalid( + self, format, value, validator_class, format_checker + ): + schema = {"format": format} + validator = validator_class(schema, format_checker=format_checker) + + with pytest.raises(ValidationError, match=f"is not a '{format}'"): + validator.validate(value) + @pytest.mark.parametrize("format", ["float", "double"]) @pytest.mark.parametrize("value", [3, 3.14, 1.0]) def test_number_float_and_double_valid(