Skip to content

Commit

Permalink
AVRO-4024: [Rust] Accept only "Nan", "INF", "-INF", "Infinity" and "-…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
martin-g committed Aug 7, 2024
1 parent 0af4e1b commit a6c8711
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,10 +933,10 @@ impl Value {
/// IEEE 754 NaN and infinities are not valid JSON numbers.
/// So they are represented in JSON as strings.
fn parse_special_float(value: &str) -> Option<f32> {
match value.trim().to_ascii_lowercase().as_str() {
"nan" | "+nan" | "-nan" => Some(f32::NAN),
"inf" | "+inf" | "infinity" | "+infinity" => Some(f32::INFINITY),
"-inf" | "-infinity" => Some(f32::NEG_INFINITY),
match value {
"NaN" => Some(f32::NAN),
"INF" | "Infinity" => Some(f32::INFINITY),
"-INF" | "-Infinity" => Some(f32::NEG_INFINITY),
_ => None,
}
}
Expand Down Expand Up @@ -3142,10 +3142,10 @@ Field with name '"b"' is not a member of the map items"#,
#[test]
fn avro_4024_resolve_double_from_unknown_string_err() -> TestResult {
let schema = Schema::parse_str(r#"{"type": "double"}"#)?;
let value = Value::String("blah".to_owned());
let value = Value::String("unknown".to_owned());
match value.resolve(&schema) {
Err(err @ Error::GetDouble(_)) => {
assert_eq!(format!("{err:?}"), r#"Double expected, got String("blah")"#);
assert_eq!(format!("{err:?}"), r#"Double expected, got String("unknown")"#);
}
other => {
panic!("Expected Error::GetDouble, got {other:?}");
Expand All @@ -3157,10 +3157,10 @@ Field with name '"b"' is not a member of the map items"#,
#[test]
fn avro_4024_resolve_float_from_unknown_string_err() -> TestResult {
let schema = Schema::parse_str(r#"{"type": "float"}"#)?;
let value = Value::String("blah".to_owned());
let value = Value::String("unknown".to_owned());
match value.resolve(&schema) {
Err(err @ Error::GetFloat(_)) => {
assert_eq!(format!("{err:?}"), r#"Float expected, got String("blah")"#);
assert_eq!(format!("{err:?}"), r#"Float expected, got String("unknown")"#);
}
other => {
panic!("Expected Error::GetFloat, got {other:?}");
Expand Down
6 changes: 3 additions & 3 deletions lang/rust/avro/tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ fn default_value_examples() -> &'static Vec<(&'static str, &'static str, Value)>
(r#""long""#, "5", Value::Long(5)),
(r#""float""#, "1.1", Value::Float(1.1)),
(r#""double""#, "1.1", Value::Double(1.1)),
(r#""float""#, r#"" +inf ""#, Value::Float(f32::INFINITY)),
(r#""float""#, r#""INF""#, Value::Float(f32::INFINITY)),
(
r#""double""#,
r#""-Infinity""#,
Value::Double(f64::NEG_INFINITY),
),
(r#""float""#, r#""-NAN""#, Value::Float(f32::NAN)),
(r#""double""#, r#""-NAN""#, Value::Double(f64::NAN)),
(r#""float""#, r#""NaN""#, Value::Float(f32::NAN)),
(r#""double""#, r#""NaN""#, Value::Double(f64::NAN)),
(
r#"{"type": "fixed", "name": "F", "size": 2}"#,
r#""a""#,
Expand Down

0 comments on commit a6c8711

Please sign in to comment.