assert_tokens reports failure to deserialize but actual deserialization works
mkpankov opened this issue · 1 comments
mkpankov commented
I have the following test code:
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate serde_test;
use serde_test::{assert_tokens, Token};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct VecWrapper(pub Vec<u8>);
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum Enum {
Str(String),
Vector(VecWrapper),
Number(f64),
Boolean(bool),
Nil,
Other,
}
fn main() {
let v = Enum::Vector(VecWrapper(vec![b'a', b'b', b'c']));
let s = serde_json::to_string(&v).unwrap();
println!("{:?}", s);
let v_: Enum = serde_json::from_str(&*s).unwrap();
assert_eq!(v, v_);
assert_tokens(
&v,
&[
Token::NewtypeStruct { name: "VecWrapper" },
Token::Seq { len: Some(3) },
Token::U8(97),
Token::U8(98),
Token::U8(99),
Token::SeqEnd,
],
);
}
With this, I get following output:
"[97,98,99]"
thread 'main' panicked at 'tokens failed to deserialize: data did not match any variant of untagged enum Enum', /home/mkpankov/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_test-1.0.42/src/assert.rs:192:19
I don't get why assert_tokens
fails while serde_json::from_str
succeeds. At the data model level everything should be the same?
dtolnay commented
Thanks! This was a bug. I published 1.0.43 with a fix and your test should work as written.