Skip to content

Commit

Permalink
Add test for serde-rs#1904
Browse files Browse the repository at this point in the history
Currently panics in derive
  • Loading branch information
Mingun committed Jul 23, 2023
1 parent 27414c9 commit 3b51b8a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test_suite/tests/regression/issue1904.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use serde_derive::Deserialize;

#[derive(Deserialize)]
pub struct Nested;

#[derive(Deserialize)]
pub enum ExternallyTagged1 {
Tuple(f64, String),
Flatten {
#[serde(flatten)]
nested: Nested,
},
}

#[derive(Deserialize)]
pub enum ExternallyTagged2 {
Flatten {
#[serde(flatten)]
nested: Nested,
},
Tuple(f64, String),
}

// Internally tagged enums cannot contain tuple variants so not tested here

#[derive(Deserialize)]
#[serde(tag = "tag", content = "content")]
pub enum AdjacentlyTagged1 {
Tuple(f64, String),
Flatten {
#[serde(flatten)]
nested: Nested,
},
}

#[derive(Deserialize)]
#[serde(tag = "tag", content = "content")]
pub enum AdjacentlyTagged2 {
Flatten {
#[serde(flatten)]
nested: Nested,
},
Tuple(f64, String),
}

#[derive(Deserialize)]
#[serde(untagged)]
pub enum Untagged1 {
Tuple(f64, String),
Flatten {
#[serde(flatten)]
nested: Nested,
},
}

#[derive(Deserialize)]
#[serde(untagged)]
pub enum Untagged2 {
Flatten {
#[serde(flatten)]
nested: Nested,
},
Tuple(f64, String),
}
50 changes: 50 additions & 0 deletions test_suite/tests/test_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,56 @@ fn test_flatten_untagged_enum() {
);
}

/// Regression test for https://github.com/serde-rs/serde/issues/1904
#[test]
fn test_enum_tuple_and_struct_with_flatten() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum Outer {
Tuple(f64, i32),
Flatten {
#[serde(flatten)]
nested: Nested,
},
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Nested {
a: i32,
b: i32,
}

assert_tokens(
&Outer::Tuple(1.2, 3),
&[
Token::TupleVariant {
name: "Outer",
variant: "Tuple",
len: 2,
},
Token::F64(1.2),
Token::I32(3),
Token::TupleVariantEnd,
],
);
assert_tokens(
&Outer::Flatten {
nested: Nested { a: 1, b: 2 },
},
&[
Token::NewtypeVariant {
name: "Outer",
variant: "Flatten",
},
Token::Map { len: None },
Token::Str("a"),
Token::I32(1),
Token::Str("b"),
Token::I32(2),
Token::MapEnd,
],
);
}

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

0 comments on commit 3b51b8a

Please sign in to comment.