Skip to content

Commit a312e56

Browse files
committed
🐛 Serialize attributes in Vector
Serialize to tags with attribute does not work if they are in a vector, while deserialize of them are supported. Simplify the serialize_element to be able to serialize regardless of the presence of attributes. Signed-off-by: Ryosuke Yasuoka <ryasuoka@redhat.com>
1 parent 920e54d commit a312e56

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

src/ser/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,6 @@ where
132132
Ok(())
133133
}
134134

135-
fn reopen_tag(&mut self) -> Result<()> {
136-
self.current_tag_attrs = Some(HashMap::new());
137-
Ok(())
138-
}
139-
140135
fn abandon_tag(&mut self) -> Result<()> {
141136
self.current_tag = "".into();
142137
self.current_tag_attrs = None;

src/ser/seq.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ impl<'ser, W: 'ser + Write> serde::ser::SerializeSeq for SeqSeralizer<'ser, W> {
2121
where
2222
T: ?Sized + Serialize,
2323
{
24-
let must_close_tag = self.ser.build_start_tag()?;
24+
let seq_tag = self.ser.current_tag();
2525
value.serialize(&mut *self.ser)?;
26-
if must_close_tag {
27-
self.ser.end_tag()?;
28-
self.ser.reopen_tag()?;
29-
}
26+
self.ser.open_tag(&seq_tag)?;
3027
Ok(())
3128
}
3229

tests/datatypes.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,63 @@ mod ser {
146146
format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, expected)
147147
);
148148
}
149+
150+
#[derive(Serialize, Debug)]
151+
#[serde(rename = "bar")]
152+
struct DummyAttributeVec<T> {
153+
bla: Vec<DummyAttribute<T>>,
154+
}
155+
156+
#[rstest]
157+
#[case(r#"<bar><bla value="apple" /><bla value="banana" /></bar>"#, vec!["apple", "banana"])]
158+
fn attribute_vec_ok(_logger: (), #[case] expected: &str, #[case] value: Vec<&str>) {
159+
let actual = to_string(&DummyAttributeVec {
160+
bla: vec![
161+
DummyAttribute { value: value[0] },
162+
DummyAttribute { value: value[1] },
163+
],
164+
})
165+
.unwrap();
166+
assert_eq!(
167+
actual,
168+
format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, expected)
169+
);
170+
}
171+
172+
#[derive(Serialize, Debug)]
173+
#[serde(rename = "bar")]
174+
struct DummyAttributeDummyVec<T> {
175+
bla: Vec<DummyAttributeDummy<T>>,
176+
}
177+
178+
#[derive(Serialize, Debug)]
179+
#[serde(rename = "bla")]
180+
struct DummyAttributeDummy<T> {
181+
#[serde(rename = "@attr")]
182+
attr: T,
183+
#[serde(rename = "foo")]
184+
dummy: Dummy<T>,
185+
}
186+
187+
#[rstest]
188+
#[case(r#"<bar><bla attr="apple"><foo>apple</foo></bla><bla attr="banana"><foo>banana</foo></bla></bar>"#, vec!["apple", "banana"])]
189+
fn attribute_vec2_ok(_logger: (), #[case] expected: &str, #[case] value: Vec<&str>) {
190+
let actual = to_string(&DummyAttributeDummyVec {
191+
bla: vec![
192+
DummyAttributeDummy {
193+
attr: value[0],
194+
dummy: Dummy { value: value[0] },
195+
},
196+
DummyAttributeDummy {
197+
attr: value[1],
198+
dummy: Dummy { value: value[1] },
199+
},
200+
],
201+
})
202+
.unwrap();
203+
assert_eq!(
204+
actual,
205+
format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, expected)
206+
);
207+
}
149208
}

0 commit comments

Comments
 (0)