Merge pull request #46 from DmitrySamoylov/fix-nested-namespaces
Fix (de)serialization for elements with nested namespaces
This commit is contained in:
commit
389077e17c
@ -98,6 +98,10 @@ impl<'de, R: Read> Deserializer<R> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn depth(&self) -> usize {
|
||||
self.depth
|
||||
}
|
||||
|
||||
pub fn set_map_value(&mut self) {
|
||||
self.is_map_value = true;
|
||||
}
|
||||
|
||||
@ -89,6 +89,41 @@ fn de_struct_namespace() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_struct_namespace_nested() {
|
||||
#[derive(YaDeserialize, Default, PartialEq, Debug)]
|
||||
#[yaserde(prefix = "nsa", namespace = "nsa: http://www.sample.com/ns/a")]
|
||||
struct A {
|
||||
#[yaserde(prefix = "nsa")]
|
||||
alpha: i32,
|
||||
}
|
||||
|
||||
#[derive(YaDeserialize, Default, PartialEq, Debug)]
|
||||
#[yaserde(prefix = "nsb", namespace = "nsb: http://www.sample.com/ns/b")]
|
||||
struct B {
|
||||
// Note that name `nested` resides in `nsb` though it has a type from `nsa`
|
||||
#[yaserde(prefix = "nsb")]
|
||||
nested: A,
|
||||
}
|
||||
|
||||
convert_and_validate!(
|
||||
r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<nsb:B
|
||||
xmlns:nsa="http://www.sample.com/ns/a"
|
||||
xmlns:nsb="http://www.sample.com/ns/b">
|
||||
<nsb:nested>
|
||||
<nsa:alpha>32</nsa:alpha>
|
||||
</nsb:nested>
|
||||
</nsb:B>
|
||||
"#,
|
||||
B,
|
||||
B {
|
||||
nested: A { alpha: 32 }
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_enum_namespace() {
|
||||
#[derive(YaDeserialize, PartialEq, Debug)]
|
||||
|
||||
@ -11,7 +11,15 @@ use yaserde::YaSerialize;
|
||||
macro_rules! convert_and_validate {
|
||||
($model: expr, $content: expr) => {
|
||||
let data: Result<String, String> = to_string(&$model);
|
||||
assert_eq!(data, Ok(String::from($content)));
|
||||
assert_eq!(
|
||||
data,
|
||||
Ok(
|
||||
String::from($content)
|
||||
.split("\n")
|
||||
.map(|s| s.trim())
|
||||
.collect::<String>()
|
||||
)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@ -152,3 +160,35 @@ fn ser_struct_default_namespace() {
|
||||
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"><item>something</item></tt>";
|
||||
convert_and_validate!(model, content);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_struct_namespace_nested() {
|
||||
#[derive(YaSerialize, Default, PartialEq, Debug)]
|
||||
#[yaserde(prefix = "nsa", namespace = "nsa: http://www.sample.com/ns/a")]
|
||||
struct A {
|
||||
#[yaserde(prefix = "nsa")]
|
||||
alpha: i32,
|
||||
}
|
||||
|
||||
#[derive(YaSerialize, Default, PartialEq, Debug)]
|
||||
#[yaserde(prefix = "nsb", namespace = "nsb: http://www.sample.com/ns/b")]
|
||||
struct B {
|
||||
// Note that name `nested` resides in `nsb` though it has a type from `nsa`
|
||||
#[yaserde(prefix = "nsb")]
|
||||
nested: A,
|
||||
}
|
||||
|
||||
convert_and_validate!(
|
||||
B {
|
||||
nested: A { alpha: 32 }
|
||||
},
|
||||
r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<nsb:B xmlns:nsb="http://www.sample.com/ns/b">
|
||||
<nsb:nested xmlns:nsa="http://www.sample.com/ns/a">
|
||||
<nsa:alpha>32</nsa:alpha>
|
||||
</nsb:nested>
|
||||
</nsb:B>
|
||||
"#
|
||||
);
|
||||
}
|
||||
|
||||
@ -17,9 +17,8 @@ pub fn parse(
|
||||
let namespaces_matches: TokenStream = namespaces
|
||||
.iter()
|
||||
.map(|(p, ns)| {
|
||||
let str_ns = ns.as_str();
|
||||
if *prefix == Some(p.to_string()) {
|
||||
Some(quote!(#str_ns => {}))
|
||||
if prefix.as_ref() == Some(p) {
|
||||
Some(quote!(#ns => {}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -382,6 +381,7 @@ pub fn parse(
|
||||
};
|
||||
debug!("Struct: start to parse {:?}", named_element);
|
||||
|
||||
if reader.depth() == 0 {
|
||||
if let Some(ref namespace) = struct_namespace {
|
||||
match namespace.as_str() {
|
||||
#namespaces_matches
|
||||
@ -390,7 +390,8 @@ pub fn parse(
|
||||
return Err(msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#variables
|
||||
#field_visitors
|
||||
|
||||
@ -405,15 +405,10 @@ pub fn serialize(
|
||||
let skip = writer.skip_start_end();
|
||||
|
||||
if !skip {
|
||||
if let Some(label) = writer.get_start_event_name() {
|
||||
let struct_start_event = XmlEvent::start_element(label.as_ref());
|
||||
let label = writer.get_start_event_name().unwrap_or_else(|| #root.to_string());
|
||||
let struct_start_event = XmlEvent::start_element(label.as_ref())#add_namespaces;
|
||||
#build_attributes
|
||||
let _ret = writer.write(struct_start_event);
|
||||
} else {
|
||||
let struct_start_event = XmlEvent::start_element(#root)#add_namespaces;
|
||||
#build_attributes
|
||||
let _ret = writer.write(struct_start_event);
|
||||
}
|
||||
}
|
||||
|
||||
#struct_inspector
|
||||
|
||||
Loading…
Reference in New Issue
Block a user