Skip to content

Commit

Permalink
AVRO-3897: [Rust] Disallow invalid namespace in fully qualified name …
Browse files Browse the repository at this point in the history
…for Rust SDK (#2570)

* AVRO-3897: [Rust] Disallow invalid namespace in fully qualified name for Rust SDK

* AVRO-3897: [Rust] Cleanup the test code

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>

* AVRO-3897: [Rust] Add a test case showing that name[space]s could be only underscores

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>

---------

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Co-authored-by: Martin Tzvetanov Grigorov <[email protected]>
  • Loading branch information
sarutak and martin-g authored Nov 2, 2023
1 parent 1652294 commit e8f48a9
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ lazy_static! {

// An optional namespace (with optional dots) followed by a name without any dots in it.
static ref SCHEMA_NAME_R: Regex =
Regex::new(r"^((?P<namespace>([A-Za-z_][A-Za-z0-9_\.]*)*)\.)?(?P<name>[A-Za-z_][A-Za-z0-9_]*)$").unwrap();
Regex::new(r"^((?P<namespace>([A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*)?)\.)?(?P<name>[A-Za-z_][A-Za-z0-9_]*)$").unwrap();

static ref FIELD_NAME_R: Regex = Regex::new(r"^[A-Za-z_][A-Za-z0-9_]*$").unwrap();

Expand Down Expand Up @@ -6195,4 +6195,37 @@ mod tests {

Ok(())
}

#[test]
fn test_avro_3897_disallow_invalid_namespaces_in_fully_qualified_name() -> TestResult {
let full_name = "ns.0.record1";
let name = Name::new(full_name);
assert!(name.is_err());
let expected =
Error::InvalidSchemaName(full_name.to_string(), SCHEMA_NAME_R.as_str()).to_string();
let err = name.map_err(|e| e.to_string()).err().unwrap();
assert_eq!(expected, err);

let full_name = "ns..record1";
let name = Name::new(full_name);
assert!(name.is_err());
let expected =
Error::InvalidSchemaName(full_name.to_string(), SCHEMA_NAME_R.as_str()).to_string();
let err = name.map_err(|e| e.to_string()).err().unwrap();
assert_eq!(expected, err);

Ok(())
}

/// A test cases showing that names and namespaces can be constructed
/// entirely by underscores.
#[test]
fn test_avro_3897_funny_valid_names_and_namespaces() -> TestResult {
for funny_name in ["_", "_._", "__._", "_.__", "_._._"] {
let name = Name::new(funny_name);
assert!(name.is_ok());
}

Ok(())
}
}

0 comments on commit e8f48a9

Please sign in to comment.