From c8175ec098e8c1498b508fe48ae0a17db81c3190 Mon Sep 17 00:00:00 2001 From: Alhuda Khan Date: Wed, 18 Nov 2026 15:12:39 +0530 Subject: [PATCH] Range-check floats in int32 and int64 format checkers is_int32 and is_int64 skipped the range check for anything that is not an int. The OAS 3.1/3.2 type checker treats integral floats as integers, so a JSON value such as 1e10 or 2147483648.0 passed {type: integer, format: int32} while 10000000000 was rejected. --- openapi_schema_validator/_format.py | 6 ++++-- tests/integration/test_validators.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) 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(