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(deserialization): nested deserialization (#200) #203

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion examples/tests/data/svd.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<device schemaversion="foo" xmlns="http://www.w3.org/2001/XMLSchema-instance" xsnonamespaceschemalocation="CMSIS-SVD.xsd">
<devattributes>
<vendor>Renesas</vendor>
Expand Down
2 changes: 2 additions & 0 deletions yaserde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Deserializer<R: Read> {
depth: usize,
reader: EventReader<R>,
peeked: Option<XmlEvent>,
pub inner_struct_label: Option<&'static str>,
}

impl<R: Read> Deserializer<R> {
Expand All @@ -26,6 +27,7 @@ impl<R: Read> Deserializer<R> {
depth: 0,
reader,
peeked: None,
inner_struct_label: None,
}
}

Expand Down
2 changes: 1 addition & 1 deletion yaserde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ macro_rules! serialize_and_validate {
log::debug!("serialize_and_validate @ {}:{}", file!(), line!());
let data: Result<String, String> = yaserde::ser::to_string(&$model);

let content = &format!(r#"<?xml version="1.0" encoding="utf-8"?>{}"#, $content);
let content = &format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, $content);
assert_eq!(
data,
Ok(content.split("\n").map(|s| s.trim()).collect::<String>())
Expand Down
4 changes: 2 additions & 2 deletions yaserde/tests/cdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ fn test_cdata_serialization() {
msgdata: "<tag>Some unescaped content</tag>".to_string(),
};
let xml_output = yaserde::ser::to_string(&test_data).expect("Serialization failed");
let expected_output = r#"<?xml version="1.0" encoding="utf-8"?><teststruct><msgdata><![CDATA[<tag>Some unescaped content</tag>]]></msgdata></teststruct>"#;
let expected_output = r#"<?xml version="1.0" encoding="UTF-8"?><teststruct><msgdata><![CDATA[<tag>Some unescaped content</tag>]]></msgdata></teststruct>"#;
assert_eq!(xml_output, expected_output);
}

#[test]
fn test_cdata_deserialization() {
init();
let xml = r#"<?xml version="1.0" encoding="utf-8"?><teststruct><msgdata><![CDATA[<tag>Some unescaped content</tag>]]></msgdata></teststruct>"#;
let xml = r#"<?xml version="1.0" encoding="UTF-8"?><teststruct><msgdata><![CDATA[<tag>Some unescaped content</tag>]]></msgdata></teststruct>"#;
let r: TestStruct = yaserde::de::from_str(xml).unwrap();
let expected_output = TestStruct {
msgdata: "<tag>Some unescaped content</tag>".to_string(),
Expand Down
160 changes: 145 additions & 15 deletions yaserde/tests/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ fn de_complex_enum() {
Dotted(u32),
}

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Black>text</Black>
Expand All @@ -598,7 +598,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Orange>text</Orange>
Expand All @@ -613,7 +613,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Red>56</Red>
Expand All @@ -628,7 +628,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Green>
Expand All @@ -646,7 +646,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Yellow>text</Yellow>
Expand All @@ -661,7 +661,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Brown>
Expand All @@ -679,7 +679,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Blue>abc</Blue>
Expand All @@ -695,7 +695,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Purple>12</Purple>
Expand All @@ -711,7 +711,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base>
<background>
<Magenta><fi>12</fi><se>23</se></Magenta>
Expand All @@ -730,7 +730,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base xmlns:ns="http://www.sample.com/ns/domain">
<background>
<NotSoCyan><fi>12</fi><se>23</se></NotSoCyan>
Expand All @@ -749,7 +749,7 @@ fn de_complex_enum() {
}
);

let content = r#"<?xml version="1.0" encoding="utf-8"?>
let content = r#"<?xml version="1.0" encoding="UTF-8"?>
<base xmlns:ns="http://www.sample.com/ns/domain">
<background>
<renamed.with.dots>54</renamed.with.dots>
Expand Down Expand Up @@ -854,7 +854,7 @@ fn de_subitem_issue_12() {
}

convert_and_validate!(
r#"<?xml version="1.0" encoding="utf-8"?>
r#"<?xml version="1.0" encoding="UTF-8"?>
<Struct>
<id>54</id>
<SubStruct>
Expand Down Expand Up @@ -884,7 +884,7 @@ fn de_subitem_issue_12_with_sub() {
}

convert_and_validate!(
r#"<?xml version="1.0" encoding="utf-8"?>
r#"<?xml version="1.0" encoding="UTF-8"?>
<Struct>
<id>54</id>
<SubStruct>
Expand All @@ -911,7 +911,7 @@ fn de_subitem_issue_12_attributes() {
}

convert_and_validate!(
r#"<?xml version="1.0" encoding="utf-8"?>
r#"<?xml version="1.0" encoding="UTF-8"?>
<Struct id="54">
<SubStruct id="86" />
</Struct>
Expand Down Expand Up @@ -940,7 +940,7 @@ fn de_subitem_issue_12_attributes_with_sub() {
}

convert_and_validate!(
r#"<?xml version="1.0" encoding="utf-8"?>
r#"<?xml version="1.0" encoding="UTF-8"?>
<Struct id="54">
<sub1 id="63" />
<sub2 id="72" />
Expand Down Expand Up @@ -1117,3 +1117,133 @@ fn de_nested_macro_rules() {

float_attrs!(f32);
}

#[test]
fn de_nested_element_equality() {
#[derive(YaDeserialize, Debug, PartialEq)]
#[yaserde(rename = "EBMLSchema")]
struct Schema {
#[yaserde(rename = "element")]
elements: Vec<Element>,
}

#[derive(YaDeserialize, Debug, PartialEq)]
#[yaserde(rename = "element")]
struct Element {
#[yaserde(rename = "documentation")]
documentation: Vec<Documentation>,
}

#[derive(YaDeserialize, Debug, PartialEq)]
#[yaserde(rename = "documentation")]
struct Documentation {
#[yaserde(text = true)]
body: String,
}

let parent = r#"<EBMLSchema>
<element>
<documentation>Test</documentation>
</element>
</EBMLSchema>"#;
let parent: Schema = yaserde::de::from_str(parent).unwrap();

let child = r#"<element>
<documentation>Test</documentation>
</element>"#;
let child: Element = yaserde::de::from_str(child).unwrap();

assert_ne!(parent.elements, vec![]);
assert_eq!(parent.elements[0], child);
}

#[test]
fn de_nested_3_levels() {
#[derive(YaSerialize, YaDeserialize, Debug, PartialEq)]
struct A {
id: String,
#[yaserde(rename = "SAME")]
many: Vec<B>,
}

#[derive(YaSerialize, YaDeserialize, Debug, PartialEq)]
struct B {
id: Option<String>,
#[yaserde(rename = "SAME")]
many: Vec<C>,
}

#[derive(YaSerialize, YaDeserialize, Debug, PartialEq)]
struct C {
id: Option<String>,
}

let content =
r#"<A><id>a1</id><SAME><id>b1</id><SAME><id>c1</id></SAME><SAME><id>c2</id></SAME></SAME></A>"#;
let model = A {
id: "a1".to_string(),
many: vec![B {
id: Some("b1".to_string()),
many: vec![
C {
id: Some("c1".to_string()),
},
C {
id: Some("c2".to_string()),
},
],
}],
};
deserialize_and_validate!(content, model, A);
}

#[test]
fn de_nested_issue_192() {
#[derive(Clone, Default, Debug, PartialEq, YaDeserialize)]
#[yaserde(
prefix = "xs",
namespaces = {
"xs" = "http://www.w3.org/2001/XMLSchema",
}
)]
pub struct XSDGroup {
#[yaserde(rename = "ref", attribute = true)]
pub reference: String,
}

#[derive(Clone, Default, Debug, PartialEq, YaDeserialize)]
#[yaserde(
rename = "sequence",
prefix = "xs",
namespaces = {
"xs" = "http://www.w3.org/2001/XMLSchema",
}
)]
pub struct Sequence {
#[yaserde(rename = "group", prefix = "xs")]
pub groups: Vec<XSDGroup>,

#[yaserde(rename = "sequence", prefix = "xs")]
pub sequences: Vec<Sequence>,
}

let content = r#"
<xsd:sequence xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:group ref="AR:AR-OBJECT"/>
<xsd:group ref="AR:AUTOSAR"/>
</xsd:sequence>
"#;
let model = Sequence {
groups: vec![
XSDGroup {
reference: "AR:AR-OBJECT".to_string(),
},
XSDGroup {
reference: "AR:AUTOSAR".to_string(),
},
],
sequences: vec![],
};

deserialize_and_validate!(content, model, Sequence);
}
2 changes: 1 addition & 1 deletion yaserde/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {

#[test]
fn deserialize_without_car() {
let person = r#"<?xml version="1.0" encoding="utf-8"?>
let person = r#"<?xml version="1.0" encoding="UTF-8"?>
<Person>
<EyeColor>brown</EyeColor>
<Age>25</Age>
Expand Down
11 changes: 5 additions & 6 deletions yaserde_derive/src/de/expand_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 @@ -160,6 +159,7 @@ pub fn parse(
}
if let Ok(::yaserde::__xml::reader::XmlEvent::StartElement { .. }) = reader.peek() {
// If substruct's start element found then deserialize substruct
reader.inner_struct_label = Some(#label_name);
let value = <#struct_name as ::yaserde::YaDeserialize>::deserialize(reader)?;
#value_label #action;
// read EndElement
Expand Down Expand Up @@ -383,7 +383,6 @@ pub fn parse(
build_code_for_unused_xml_events(&call_flatten_visitors)
};

let flatten = root_attributes.flatten;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

quote! {
Expand All @@ -392,6 +391,7 @@ pub fn parse(
fn deserialize<R: ::std::io::Read>(
reader: &mut ::yaserde::de::Deserializer<R>,
) -> ::std::result::Result<Self, ::std::string::String> {
let root_label= reader.inner_struct_label.take().unwrap_or(#root);
let (named_element, struct_namespace) =
if let ::yaserde::__xml::reader::XmlEvent::StartElement { name, .. } = reader.peek()?.to_owned() {
(name.local_name.to_owned(), name.namespace.clone())
Expand Down Expand Up @@ -421,7 +421,7 @@ pub fn parse(
match event {
::yaserde::__xml::reader::XmlEvent::StartElement{ref name, ref attributes, ..} => {
let namespace = name.namespace.clone().unwrap_or_default();
if depth == 0 && name.local_name == #root && namespace.as_str() == #root_namespace {
if depth == 0 && name.local_name == root_label {
// 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()?;
Expand Down Expand Up @@ -457,9 +457,8 @@ pub fn parse(
depth -= 1;
}
::yaserde::__xml::reader::XmlEvent::EndDocument => {
if #flatten {
break;
}
// once we receive this once, we will keep getting it, potentially looping forever
break;
}
::yaserde::__xml::reader::XmlEvent::Characters(ref text_content) => {
#set_text
Expand Down
Loading