Skip to content

Commit fe3f75e

Browse files
authored
fix(quick-xml): Fix bug in sub-access of attributes in elements. (#123)
1 parent 348bcb7 commit fe3f75e

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

xmlity-quick-xml/src/de.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<'de> de::AttributesAccess<'de> for SubAttributesAccess<'_, 'de> {
623623
deserializer: self.deserializer,
624624
bytes_start: self.bytes_start,
625625
attribute_index: self.attribute_index,
626-
write_attribute_to: self.write_attribute_to,
626+
write_attribute_to: &mut self.attribute_index,
627627
})
628628
}
629629
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use xmlity::{
2+
de::DeserializationGroupBuilder,
3+
value::{XmlAttribute, XmlText},
4+
DeserializationGroup, Deserialize, ExpandedName, LocalName, SerializationGroup, Serialize,
5+
};
6+
7+
use crate::define_test;
8+
9+
#[derive(Debug, PartialEq)]
10+
pub struct AnyAttributes {
11+
/// The attributes of the element.
12+
pub attributes: Vec<XmlAttribute>,
13+
}
14+
15+
impl<'de> DeserializationGroupBuilder<'de> for AnyAttributes {
16+
type Value = Self;
17+
18+
fn contribute_attributes<D: xmlity::de::AttributesAccess<'de>>(
19+
&mut self,
20+
mut access: D,
21+
) -> Result<bool, D::Error> {
22+
if let Ok(Some(attr)) = access.next_attribute::<XmlAttribute>() {
23+
self.attributes.push(attr);
24+
Ok(true)
25+
} else {
26+
Ok(false)
27+
}
28+
}
29+
30+
fn attributes_done(&self) -> bool {
31+
false
32+
}
33+
34+
fn finish<E: xmlity::de::Error>(self) -> Result<Self::Value, E> {
35+
Ok(self)
36+
}
37+
}
38+
39+
impl<'de> DeserializationGroup<'de> for AnyAttributes {
40+
type Builder = Self;
41+
42+
fn builder() -> Self::Builder {
43+
Self {
44+
attributes: Vec::new(),
45+
}
46+
}
47+
}
48+
49+
impl SerializationGroup for AnyAttributes {
50+
fn serialize_attributes<S: xmlity::ser::SerializeAttributes>(
51+
&self,
52+
serializer: &mut S,
53+
) -> Result<(), S::Error> {
54+
self.attributes
55+
.iter()
56+
.try_for_each(|attr| serializer.serialize_attribute(attr).map(|_| ()))
57+
}
58+
59+
fn serialize_children<S: xmlity::ser::SerializeSeq>(
60+
&self,
61+
serializer: &mut S,
62+
) -> Result<(), S::Error> {
63+
let _ = serializer;
64+
65+
Ok(())
66+
}
67+
}
68+
69+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
70+
#[xelement(name = "a")]
71+
pub struct A {
72+
#[xgroup]
73+
any_attributes: AnyAttributes,
74+
}
75+
76+
define_test!(
77+
any_attributes_element,
78+
[(
79+
A {
80+
any_attributes: AnyAttributes {
81+
attributes: vec![XmlAttribute::new(
82+
ExpandedName::new(LocalName::new_dangerous("test"), None),
83+
XmlText::new("testVal")
84+
)],
85+
}
86+
},
87+
r###"<a test="testVal"/>"###
88+
)]
89+
);
90+
91+
#[derive(Debug, PartialEq, SerializationGroup, DeserializationGroup)]
92+
pub struct B {
93+
#[xgroup]
94+
any_attributes: AnyAttributes,
95+
}
96+
97+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
98+
#[xelement(name = "a")]
99+
pub struct C {
100+
#[xgroup]
101+
b: B,
102+
}
103+
104+
define_test!(
105+
any_attributes_element_in_group,
106+
[(
107+
C {
108+
b: B {
109+
any_attributes: AnyAttributes {
110+
attributes: vec![XmlAttribute::new(
111+
ExpandedName::new(LocalName::new_dangerous("test"), None),
112+
XmlText::new("testVal")
113+
)],
114+
}
115+
}
116+
},
117+
r###"<a test="testVal"/>"###
118+
)]
119+
);

xmlity-quick-xml/tests/elements/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
pub mod allow_unknown;
2+
pub mod any_attributes;
23
pub mod attribute;
4+
pub mod attribute_namespace;
35
pub mod basic;
46
pub mod default;
57
pub mod extendable;
68
pub mod generics;
9+
pub mod group_element_equivalent;
710
pub mod ignore_comments;
811
pub mod ignore_whitespace;
912
pub mod inline_attribute_declarations;
@@ -16,6 +19,3 @@ pub mod option;
1619
pub mod single_namespace;
1720
pub mod skip_serializing_if;
1821
pub mod strict_order;
19-
20-
pub mod attribute_namespace;
21-
pub mod group_element_equivalent;

0 commit comments

Comments
 (0)