From 1446923a95e80c81cdaf28f5625aef8d0ce3e73d Mon Sep 17 00:00:00 2001 From: limingyi Date: Mon, 16 Sep 2024 15:23:27 +0800 Subject: [PATCH] fix: fix nesting struct parsing bug issue #192 Signed-off-by: limingyi --- yaserde_derive/src/de/expand_struct.rs | 5 +++-- yaserde_derive/src/de/mod.rs | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/yaserde_derive/src/de/expand_struct.rs b/yaserde_derive/src/de/expand_struct.rs index 5000e8f..fca7486 100644 --- a/yaserde_derive/src/de/expand_struct.rs +++ b/yaserde_derive/src/de/expand_struct.rs @@ -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, @@ -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 diff --git a/yaserde_derive/src/de/mod.rs b/yaserde_derive/src/de/mod.rs index 31453f7..e0da4b4 100644 --- a/yaserde_derive/src/de/mod.rs +++ b/yaserde_derive/src/de/mod.rs @@ -14,15 +14,22 @@ pub fn expand_derive_deserialize(ast: &syn::DeriveInput) -> Result { - 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)