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

Always capture "$value" key when text node is appeared in the map #237

Merged
merged 1 commit into from
Oct 12, 2020
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ memchr = "2.3.3"

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
serde-value = "0.7"
regex = "1"

[lib]
Expand Down
22 changes: 21 additions & 1 deletion src/de/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,27 @@ impl<'a, 'de, R: BufRead> de::MapAccess<'de> for MapAccess<'a, R> {
} else {
// try getting from events (<key>value</key>)
match self.de.peek()? {
Some(Event::Text(_)) | Some(Event::Start(_)) if has_value_field => {
Some(Event::Text(_)) => {
self.value = MapValue::InnerValue;
seed.deserialize(INNER_VALUE.into_deserializer()).map(Some)
}
// Used to deserialize collections of enums, like:
// <root>
// <A/>
// <B/>
// <C/>
// </root>
//
// into
//
// enum Enum { A, B, С }
// struct Root {
// #[serde(rename = "$value")]
// items: Vec<Enum>,
// }
// TODO: This should be handled by #[serde(flatten)]
// See https://github.com/serde-rs/serde/issues/1905
Some(Event::Start(_)) if has_value_field => {
self.value = MapValue::InnerValue;
seed.deserialize(INNER_VALUE.into_deserializer()).map(Some)
}
Expand Down
58 changes: 52 additions & 6 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,19 +545,21 @@ mod tests {

#[derive(Debug, Deserialize, PartialEq)]
struct MyEnums {
// TODO: This should be #[serde(flatten)], but right now serde don't support flattening of sequences
// See https://github.com/serde-rs/serde/issues/1905
#[serde(rename = "$value")]
items: Vec<MyEnum>,
}

#[test]
fn collection_of_enums() {
let s = r##"
<enums>
<A>test</A>
<B name="hello" flag="t" />
<C />
</enums>
"##;
<enums>
<A>test</A>
<B name="hello" flag="t" />
<C />
</enums>
"##;

let project: MyEnums = from_str(s).unwrap();

Expand Down Expand Up @@ -616,4 +618,48 @@ mod tests {
}
);
}

/// Test for https://github.com/tafia/quick-xml/issues/231
#[test]
fn implicit_value() {
use serde_value::Value;

let s = r#"<root>content</root>"#;
let item: Value = from_str(s).unwrap();

assert_eq!(
item,
Value::Map(vec![
(Value::String("$value".into()), Value::String("content".into()))
].into_iter().collect())
);
}

#[test]
fn explicit_value() {
#[derive(Debug, Deserialize, PartialEq)]
struct Item {
#[serde(rename = "$value")]
content: String,
}

let s = r#"<root>content</root>"#;
let item: Item = from_str(s).unwrap();

assert_eq!(
item,
Item { content: "content".into() }
);
}

#[test]
fn without_value() {
#[derive(Debug, Deserialize, PartialEq)]
struct Item;

let s = r#"<root>content</root>"#;
let item: Item = from_str(s).unwrap();

assert_eq!(item, Item);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ extern crate encoding_rs;
extern crate memchr;
#[cfg(feature = "serialize")]
extern crate serde;
#[cfg(all(test, feature = "serialize"))]
extern crate serde_value;

#[cfg(feature = "serialize")]
pub mod de;
Expand Down