Skip to content

Commit 5631590

Browse files
committed
AVRO-4024: [Rust] Accept only "Nan", "INF", "-INF", "Infinity" and "-Infinity"
This is what the Java SDK (via Jackson library) supports (#3066). This is what the C# SDK also would support (#3070) Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
1 parent 0af4e1b commit 5631590

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lang/rust/avro/src/types.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -933,10 +933,10 @@ impl Value {
933933
/// IEEE 754 NaN and infinities are not valid JSON numbers.
934934
/// So they are represented in JSON as strings.
935935
fn parse_special_float(value: &str) -> Option<f32> {
936-
match value.trim().to_ascii_lowercase().as_str() {
937-
"nan" | "+nan" | "-nan" => Some(f32::NAN),
938-
"inf" | "+inf" | "infinity" | "+infinity" => Some(f32::INFINITY),
939-
"-inf" | "-infinity" => Some(f32::NEG_INFINITY),
936+
match value {
937+
"NaN" => Some(f32::NAN),
938+
"INF" | "Infinity" => Some(f32::INFINITY),
939+
"-INF" | "-Infinity" => Some(f32::NEG_INFINITY),
940940
_ => None,
941941
}
942942
}
@@ -3142,10 +3142,13 @@ Field with name '"b"' is not a member of the map items"#,
31423142
#[test]
31433143
fn avro_4024_resolve_double_from_unknown_string_err() -> TestResult {
31443144
let schema = Schema::parse_str(r#"{"type": "double"}"#)?;
3145-
let value = Value::String("blah".to_owned());
3145+
let value = Value::String("unknown".to_owned());
31463146
match value.resolve(&schema) {
31473147
Err(err @ Error::GetDouble(_)) => {
3148-
assert_eq!(format!("{err:?}"), r#"Double expected, got String("blah")"#);
3148+
assert_eq!(
3149+
format!("{err:?}"),
3150+
r#"Double expected, got String("unknown")"#
3151+
);
31493152
}
31503153
other => {
31513154
panic!("Expected Error::GetDouble, got {other:?}");
@@ -3157,10 +3160,13 @@ Field with name '"b"' is not a member of the map items"#,
31573160
#[test]
31583161
fn avro_4024_resolve_float_from_unknown_string_err() -> TestResult {
31593162
let schema = Schema::parse_str(r#"{"type": "float"}"#)?;
3160-
let value = Value::String("blah".to_owned());
3163+
let value = Value::String("unknown".to_owned());
31613164
match value.resolve(&schema) {
31623165
Err(err @ Error::GetFloat(_)) => {
3163-
assert_eq!(format!("{err:?}"), r#"Float expected, got String("blah")"#);
3166+
assert_eq!(
3167+
format!("{err:?}"),
3168+
r#"Float expected, got String("unknown")"#
3169+
);
31643170
}
31653171
other => {
31663172
panic!("Expected Error::GetFloat, got {other:?}");

lang/rust/avro/tests/io.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,22 @@ fn default_value_examples() -> &'static Vec<(&'static str, &'static str, Value)>
107107
(r#""long""#, "5", Value::Long(5)),
108108
(r#""float""#, "1.1", Value::Float(1.1)),
109109
(r#""double""#, "1.1", Value::Double(1.1)),
110-
(r#""float""#, r#"" +inf ""#, Value::Float(f32::INFINITY)),
110+
(r#""float""#, r#""INF""#, Value::Float(f32::INFINITY)),
111+
(r#""double""#, r#""INF""#, Value::Double(f64::INFINITY)),
112+
(r#""float""#, r#""Infinity""#, Value::Float(f32::INFINITY)),
113+
(
114+
r#""float""#,
115+
r#""-Infinity""#,
116+
Value::Float(f32::NEG_INFINITY),
117+
),
118+
(r#""double""#, r#""Infinity""#, Value::Double(f64::INFINITY)),
111119
(
112120
r#""double""#,
113121
r#""-Infinity""#,
114122
Value::Double(f64::NEG_INFINITY),
115123
),
116-
(r#""float""#, r#""-NAN""#, Value::Float(f32::NAN)),
117-
(r#""double""#, r#""-NAN""#, Value::Double(f64::NAN)),
124+
(r#""float""#, r#""NaN""#, Value::Float(f32::NAN)),
125+
(r#""double""#, r#""NaN""#, Value::Double(f64::NAN)),
118126
(
119127
r#"{"type": "fixed", "name": "F", "size": 2}"#,
120128
r#""a""#,

0 commit comments

Comments
 (0)