Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Commit

Permalink
Fix struct newtype deserialization (and add tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsieber committed Nov 19, 2018
1 parent 199ed02 commit 0ecc730
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ impl<'de> de::Deserializer<'de> for Part<'de> {
visitor.visit_enum(ValueEnumAccess(self.0))
}

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

forward_to_deserialize_any! {
char
str
Expand All @@ -216,7 +226,6 @@ impl<'de> de::Deserializer<'de> for Part<'de> {
bytes
byte_buf
unit_struct
newtype_struct
tuple_struct
struct
identifier
Expand Down
11 changes: 11 additions & 0 deletions tests/test_deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ extern crate serde_urlencoded;
#[macro_use]
extern crate serde_derive;

#[derive(Deserialize, Debug, PartialEq)]
struct NewType<T>(T);

#[test]
fn deserialize_newtype_i32() {
let result = vec![("field".to_owned(), NewType(11))];

assert_eq!(serde_urlencoded::from_str("field=11"),
Ok(result));
}

#[test]
fn deserialize_bytes() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
Expand Down
12 changes: 12 additions & 0 deletions tests/test_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ extern crate serde_urlencoded;
#[macro_use]
extern crate serde_derive;

#[derive(Serialize)]
struct NewType<T>(T);

#[test]
fn serialize_newtype_i32() {
let params = &[("field", Some(NewType(11))),];
assert_eq!(
serde_urlencoded::to_string(params),
Ok("field=11".to_owned())
);
}

#[test]
fn serialize_option_map_int() {
let params = &[("first", Some(23)), ("middle", None), ("last", Some(42))];
Expand Down

0 comments on commit 0ecc730

Please sign in to comment.