Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions openapi_schema_validator/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down