From c6301fe5966b98b11970df49129e34f76616c9dc Mon Sep 17 00:00:00 2001 From: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com> Date: Tue, 5 Mar 2024 11:56:21 -0600 Subject: [PATCH] Fix parsing BigInt from str (#1204) --- src/input/input_json.rs | 5 +---- src/input/input_string.rs | 7 ++----- tests/validators/test_int.py | 8 ++++++++ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/input/input_json.rs b/src/input/input_json.rs index cd4ac919c..dde73cf65 100644 --- a/src/input/input_json.rs +++ b/src/input/input_json.rs @@ -370,10 +370,7 @@ impl<'a> Input<'a> for String { } fn validate_int(&'a self, _strict: bool) -> ValResult>> { - match self.parse() { - Ok(i) => Ok(ValidationMatch::lax(EitherInt::I64(i))), - Err(_) => Err(ValError::new(ErrorTypeDefaults::IntParsing, self)), - } + str_as_int(self, self).map(ValidationMatch::lax) } fn validate_float(&'a self, _strict: bool) -> ValResult>> { diff --git a/src/input/input_string.rs b/src/input/input_string.rs index cd5931b24..f6c33d784 100644 --- a/src/input/input_string.rs +++ b/src/input/input_string.rs @@ -11,7 +11,7 @@ use crate::validators::decimal::create_decimal; use super::datetime::{ bytes_as_date, bytes_as_datetime, bytes_as_time, bytes_as_timedelta, EitherDate, EitherDateTime, EitherTime, }; -use super::shared::{str_as_bool, str_as_float}; +use super::shared::{str_as_bool, str_as_float, str_as_int}; use super::{ BorrowInput, EitherBytes, EitherFloat, EitherInt, EitherString, EitherTimedelta, GenericArguments, GenericIterable, GenericIterator, GenericMapping, Input, ValidationMatch, @@ -112,10 +112,7 @@ impl<'a> Input<'a> for StringMapping<'a> { fn validate_int(&'a self, _strict: bool) -> ValResult>> { match self { - Self::String(s) => match py_string_str(s)?.parse() { - Ok(i) => Ok(ValidationMatch::strict(EitherInt::I64(i))), - Err(_) => Err(ValError::new(ErrorTypeDefaults::IntParsing, self)), - }, + Self::String(s) => str_as_int(self, py_string_str(s)?).map(ValidationMatch::strict), Self::Mapping(_) => Err(ValError::new(ErrorTypeDefaults::IntType, self)), } } diff --git a/tests/validators/test_int.py b/tests/validators/test_int.py index 24928718d..c5ccd7957 100644 --- a/tests/validators/test_int.py +++ b/tests/validators/test_int.py @@ -504,3 +504,11 @@ def test_allow_inf_nan_false_json() -> None: v.validate_json('Infinity') with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'): v.validate_json('-Infinity') + + +def test_json_big_int_key(): + v = SchemaValidator({'type': 'dict', 'keys_schema': {'type': 'int'}, 'values_schema': {'type': 'str'}}) + big_integer = 1433352099889938534014333520998899385340 + assert v.validate_python({big_integer: 'x'}) == {big_integer: 'x'} + assert v.validate_json('{"' + str(big_integer) + '": "x"}') == {big_integer: 'x'} + assert v.validate_strings({str(big_integer): 'x'}) == {big_integer: 'x'}