Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion avro/src/schema/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,24 @@ impl<'de> Deserialize<'de> for Name {
}

/// Newtype pattern for `Name` to better control the `serde_json::Value` representation.
///
/// Aliases are serialized as an array of plain strings in the JSON representation.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Alias(Name);

impl Alias {
pub fn new(name: &str) -> AvroResult<Self> {
pub fn new(name: impl Into<String> + AsRef<str>) -> AvroResult<Self> {
Name::new(name).map(Self)
}

/// Create a new `Alias` using the namespace from `enclosing_namespace` if absent.
pub fn new_with_enclosing_namespace(
name: impl Into<String> + AsRef<str>,
enclosing_namespace: NamespaceRef,
) -> AvroResult<Self> {
Name::new_with_enclosing_namespace(name, enclosing_namespace).map(Self)
}

pub fn name(&self) -> &str {
self.0.name()
}
Expand Down
11 changes: 2 additions & 9 deletions avro/src/schema/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,19 +803,12 @@ impl Parser {
aliases
.map(|aliases| {
aliases
.iter()
.into_iter()
.map(|alias| {
// An alias that is not a valid Avro name is a schema
// error — propagate it instead of unwrapping (which
// would panic on attacker-controlled schema JSON).
if alias.contains('.') {
Alias::new(alias.as_str())
} else {
match namespace {
Some(ns) => Alias::new(&format!("{ns}.{alias}")),
None => Alias::new(alias.as_str()),
}
}
Alias::new_with_enclosing_namespace(alias, namespace)
})
.collect::<AvroResult<Vec<_>>>()
})
Expand Down
Loading