This is an automated email from the ASF dual-hosted git repository.
kriskras99 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro-rs.git
The following commit(s) were added to refs/heads/main by this push:
new debcc98 fix: Resolving a `Long` to an `Int` silently truncates (#392)
debcc98 is described below
commit debcc98ab9a435d516a3c92f9a3e0dd9435b6e4b
Author: Kriskras99 <[email protected]>
AuthorDate: Wed Jan 7 16:05:16 2026 +0100
fix: Resolving a `Long` to an `Int` silently truncates (#392)
* fix: Resolving a `Long` to an `Int` silently truncates
* test: Add test for resolving `Long` to `Int`
---
avro/src/types.rs | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/avro/src/types.rs b/avro/src/types.rs
index 02a5a8e..85a26ca 100644
--- a/avro/src/types.rs
+++ b/avro/src/types.rs
@@ -903,7 +903,10 @@ impl Value {
fn resolve_int(self) -> Result<Self, Error> {
match self {
Value::Int(n) => Ok(Value::Int(n)),
- Value::Long(n) => Ok(Value::Int(n as i32)),
+ Value::Long(n) => {
+ let n = i32::try_from(n).map_err(|e| Details::ZagI32(e, n))?;
+ Ok(Value::Int(n))
+ }
other => Err(Details::GetInt(other).into()),
}
}
@@ -3471,4 +3474,17 @@ Field with name '"b"' is not a member of the map items"#,
Ok(())
}
+
+ #[test]
+ fn avro_rs_392_resolve_long_to_int() {
+ // Values that are valid as in i32 should work
+ let value = Value::Long(0);
+ value.resolve(&Schema::Int).unwrap();
+ // Values that are outside the i32 range should not
+ let value = Value::Long(i64::MAX);
+ assert!(matches!(
+ value.resolve(&Schema::Int).unwrap_err().details(),
+ Details::ZagI32(_, _)
+ ));
+ }
}