Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nesting struct parsing bug #193

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions yaserde_derive/src/de/expand_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use syn::{DataStruct, Generics, Ident};
pub fn parse(
data_struct: &DataStruct,
name: &Ident,
root_namespace: &str,
root: &str,
root_attributes: &YaSerdeAttribute,
generics: &Generics,
Expand Down Expand Up @@ -419,13 +420,13 @@ pub fn parse(
);
match event {
::yaserde::__xml::reader::XmlEvent::StartElement{ref name, ref attributes, ..} => {
if depth == 0 && name.local_name == #root {
let namespace = name.namespace.clone().unwrap_or_default();
if depth == 0 && name.local_name == #root && namespace.as_str() == #root_namespace {
// Consume root element. We must do this first. In the case it shares a name with a child element, we don't
// want to prematurely match the child element below.
let event = reader.next_event()?;
#write_unused
} else {
let namespace = name.namespace.clone().unwrap_or_default();

match (namespace.as_str(), name.local_name.as_str()) {
#call_visitors
Expand Down
19 changes: 13 additions & 6 deletions yaserde_derive/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ pub fn expand_derive_deserialize(ast: &syn::DeriveInput) -> Result<TokenStream,

let root_attributes = YaSerdeAttribute::parse(attrs);

let root_name = format!(
"{}{}",
root_attributes.prefix_namespace(),
root_attributes.xml_element_name(name)
);
let root_name = root_attributes.xml_element_name(name);
let root_namespace = root_attributes
.namespaces
.iter()
.find_map(|(prefix, namespace)| {
if root_attributes.prefix.eq(prefix) {
Some(namespace.clone())
} else {
None
}
})
.unwrap_or_default();

let impl_block = match *data {
syn::Data::Struct(ref data_struct) => {
expand_struct::parse(data_struct, name, &root_name, &root_attributes, generics)
expand_struct::parse(data_struct, name, &root_namespace ,&root_name, &root_attributes, generics)
}
syn::Data::Enum(ref data_enum) => {
expand_enum::parse(data_enum, name, &root_name, &root_attributes, generics)
Expand Down
Loading