Skip to content

Commit

Permalink
Fix deserialization of single element to Option<Vec<NonString>> value
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Mar 28, 2024
1 parent 63c9c33 commit 2aca384
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/de/part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'de> de::Deserializer<'de> for Part<'de> {
where
V: de::Visitor<'de>,
{
visitor.visit_seq(PartSeqAccess(Some(self.0)))
visitor.visit_seq(PartSeqAccess(Some(self)))
}

forward_to_deserialize_any! {
Expand Down Expand Up @@ -131,7 +131,7 @@ impl<'de> de::EnumAccess<'de> for Part<'de> {
}
}

struct PartSeqAccess<'de>(Option<Cow<'de, str>>);
struct PartSeqAccess<'de>(Option<Part<'de>>);

impl<'de> de::SeqAccess<'de> for PartSeqAccess<'de> {
type Error = Error;
Expand All @@ -141,7 +141,7 @@ impl<'de> de::SeqAccess<'de> for PartSeqAccess<'de> {
T: de::DeserializeSeed<'de>,
{
match self.0.take() {
Some(value) => seed.deserialize(value.into_deserializer()).map(Some),
Some(value) => seed.deserialize(value).map(Some),
None => Ok(None),
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/de/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ fn deserialize_option_vec() {
);
}

#[test]
fn deserialize_option_vec_int() {
#[derive(Deserialize, PartialEq, Debug)]
struct Form {
value: Option<Vec<i32>>,
}

assert_eq!(super::from_str(""), Ok(Form { value: None }));
assert_eq!(super::from_str("value=0"), Ok(Form { value: Some(vec![0]) }));
assert_eq!(super::from_str("value=3&value=-1"), Ok(Form { value: Some(vec![3, -1]) }));
}

#[test]
fn deserialize_option_no_value() {
#[derive(Deserialize, PartialEq, Debug)]
Expand Down

0 comments on commit 2aca384

Please sign in to comment.