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 using incorrect deserializer when deserialize Option values in maps and structs #548

Merged
merged 3 commits into from
Feb 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

- [#537]: Restore ability to deserialize attributes that represents XML namespace
mappings (`xmlns:xxx`) that was broken since [#490]
- [#510]: Fix an error of deserialization of `Option<T>` fields where `T` is some
sequence type (for example, `Vec` or tuple)

### Misc Changes

[externally tagged]: https://serde.rs/enum-representations.html#externally-tagged
[#490]: https://github.com/tafia/quick-xml/pull/490
[#510]: https://github.com/tafia/quick-xml/issues/510
[#537]: https://github.com/tafia/quick-xml/issues/537
[#541]: https://github.com/tafia/quick-xml/pull/541

Expand Down
42 changes: 4 additions & 38 deletions src/de/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,7 @@ where

deserialize_primitives!(mut);

forward!(deserialize_option);
forward!(deserialize_unit);
forward!(deserialize_unit_struct(name: &'static str));
forward!(deserialize_newtype_struct(name: &'static str));

forward!(deserialize_map);
forward!(deserialize_struct(
Expand All @@ -499,25 +496,11 @@ where
forward!(deserialize_any);
forward!(deserialize_ignored_any);

/// Tuple representation is the same as [sequences](#method.deserialize_seq).
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, DeError>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}

/// Named tuple representation is the same as [unnamed tuples](#method.deserialize_tuple).
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_tuple(len, visitor)
deserialize_option!(self.map.de, self, visitor)
}

/// Deserializes each `<tag>` in
Expand Down Expand Up @@ -758,10 +741,7 @@ where

deserialize_primitives!(mut);

forward!(deserialize_option);
forward!(deserialize_unit);
forward!(deserialize_unit_struct(name: &'static str));
forward!(deserialize_newtype_struct(name: &'static str));

forward!(deserialize_map);
forward!(deserialize_struct(
Expand All @@ -777,25 +757,11 @@ where
forward!(deserialize_any);
forward!(deserialize_ignored_any);

/// Representation of tuples the same as [sequences](#method.deserialize_seq).
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}

/// Representation of named tuples the same as [unnamed tuples](#method.deserialize_tuple).
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, DeError>
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_tuple(len, visitor)
deserialize_option!(self.map.de, self, visitor)
}

/// This method deserializes a sequence inside of element that itself is a
Expand Down
113 changes: 56 additions & 57 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,6 @@
//! Use the [`Option`]. In that case inner array will always contains at least one
//! element after deserialization:
//! ```ignore
//! // FIXME: #510,
//! // UnexpectedEnd([97, 110, 121, 45, 116, 97, 103])
//! # use pretty_assertions::assert_eq;
//! # use serde::Deserialize;
//! # type Item = ();
Expand All @@ -935,10 +933,6 @@
//! # quick_xml::de::from_str(r#"<any-tag><item/><item/><item/></any-tag>"#).unwrap(),
//! # );
//! ```
//! <div style="background:rgba(80, 240, 100, 0.20);padding:0.75em;">
//!
//! Currently not working. The bug is tracked in [#510].
//! </div>
//!
//! See also [Frequently Used Patterns](#element-lists).
//!
Expand Down Expand Up @@ -1728,7 +1722,6 @@
//! [`deserialize_with`]: https://serde.rs/field-attrs.html#deserialize_with
//! [#474]: https://github.com/tafia/quick-xml/issues/474
//! [#497]: https://github.com/tafia/quick-xml/issues/497
//! [#510]: https://github.com/tafia/quick-xml/issues/510

// Macros should be defined before the modules that using them
// Also, macros should be imported before using them
Expand Down Expand Up @@ -1822,6 +1815,50 @@ macro_rules! deserialize_primitives {
self.deserialize_bytes(visitor)
}

/// Representation of the named units the same as [unnamed units](#method.deserialize_unit)
fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_unit(visitor)
}

fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_tuple(1, visitor)
}

/// Representation of tuples the same as [sequences](#method.deserialize_seq).
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}

/// Representation of named tuples the same as [unnamed tuples](#method.deserialize_tuple).
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_tuple(len, visitor)
}

/// Identifiers represented as [strings](#method.deserialize_str).
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, DeError>
where
Expand All @@ -1832,6 +1869,17 @@ macro_rules! deserialize_primitives {
};
}

macro_rules! deserialize_option {
($de:expr, $deserializer:ident, $visitor:ident) => {
match $de.peek()? {
DeEvent::Text(t) if t.is_empty() => $visitor.visit_none(),
DeEvent::CData(t) if t.is_empty() => $visitor.visit_none(),
DeEvent::Eof => $visitor.visit_none(),
_ => $visitor.visit_some($deserializer),
}
};
}

mod key;
mod map;
mod simple_type;
Expand Down Expand Up @@ -2420,50 +2468,6 @@ where
}
}

/// Representation of the names units the same as [unnamed units](#method.deserialize_unit)
fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_unit(visitor)
}

fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_tuple(1, visitor)
}

/// Representation of tuples the same as [sequences](#method.deserialize_seq).
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}

/// Representation of named tuples the same as [unnamed tuples](#method.deserialize_tuple).
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_tuple(len, visitor)
}

fn deserialize_enum<V>(
self,
_name: &'static str,
Expand Down Expand Up @@ -2494,12 +2498,7 @@ where
where
V: Visitor<'de>,
{
match self.peek()? {
DeEvent::Text(t) if t.is_empty() => visitor.visit_none(),
DeEvent::CData(t) if t.is_empty() => visitor.visit_none(),
DeEvent::Eof => visitor.visit_none(),
_ => visitor.visit_some(self),
}
deserialize_option!(self, self, visitor)
}

/// Always call `visitor.visit_unit()` because returned value ignored in any case.
Expand Down
42 changes: 42 additions & 0 deletions tests/serde-issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,48 @@ fn issue500() {
);
}

/// Regression test for https://github.com/tafia/quick-xml/issues/510.
#[test]
fn issue510() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename = "ENTRY")]
struct Entry {
#[serde(rename = "CUE_V2")]
cues: Option<Vec<Cue>>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
// #[serde_with::serde_as]
struct Cue {
#[serde(rename = "@NAME")]
name: String,
}

let data: Entry = from_str(
"\
<ENTRY>\
<CUE_V2 NAME='foo'></CUE_V2>\
<CUE_V2 NAME='bar'></CUE_V2>\
</ENTRY>\
",
)
.unwrap();

assert_eq!(
data,
Entry {
cues: Some(vec![
Cue {
name: "foo".to_string(),
},
Cue {
name: "bar".to_string(),
},
]),
}
);
}

/// Regression test for https://github.com/tafia/quick-xml/issues/537.
///
/// This test checks that special `xmlns:xxx` attributes uses full name of
Expand Down