Skip to content

Commit

Permalink
Use local name when choosing enum variant based on element name
Browse files Browse the repository at this point in the history
This is consistent to how struct fields are identified in
`MapAccess::next_key_seed` as demonstrated by the two test cases.
  • Loading branch information
adamreichold committed Jul 29, 2022
1 parent e98ad65 commit e739fdc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
when an enum variant represented as a `Text` event (i.e. `<xml>tag</xml>`)
and a document encoding is not an UTF-8
- [#434]: Fixed incorrect error generated in some cases by serde deserializer
- [#445]: Use local name without namespace prefix when selecting enum variants based on element names

### Misc Changes

Expand Down
2 changes: 1 addition & 1 deletion src/de/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
// Escape sequences does not processed inside CDATA section
DeEvent::CData(t) => EscapedDeserializer::new(Cow::Borrowed(t), decoder, false),
DeEvent::Start(e) => {
EscapedDeserializer::new(Cow::Borrowed(e.name().into_inner()), decoder, false)
EscapedDeserializer::new(Cow::Borrowed(e.local_name().into_inner()), decoder, false)
}
_ => {
return Err(DeError::Unsupported(
Expand Down
32 changes: 32 additions & 0 deletions tests/serde-de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3732,6 +3732,22 @@ mod struct_ {
);
}

#[test]
fn namespaces() {
let data: Struct = from_str(
// Comment for prevent unnecessary formatting - we use the same style in all tests
r#"<root xmlns:namespace="http://name.space"><namespace:float>42</namespace:float><string>answer</string></root>"#,
)
.unwrap();
assert_eq!(
data,
Struct {
float: 42.0,
string: "answer".into()
}
);
}

maplike_errors!(Struct);
}

Expand Down Expand Up @@ -3927,6 +3943,22 @@ mod enum_ {
}
);
}

#[test]
fn namespaces() {
let data: Node = from_str(
// Comment for prevent unnecessary formatting - we use the same style in all tests
r#"<namespace:Struct xmlns:namespace="http://name.space"><float>42</float><string>answer</string></namespace:Struct>"#,
)
.unwrap();
assert_eq!(
data,
Node::Struct {
float: 42.0,
string: "answer".into()
}
);
}
}

mod nested_struct {
Expand Down

0 comments on commit e739fdc

Please sign in to comment.