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

assert_tokens reports failure to deserialize but actual deserialization works #17

Closed
mkpankov opened this issue Apr 23, 2018 · 1 comment · Fixed by serde-rs/serde#1234
Labels
bug Something isn't working

Comments

@mkpankov
Copy link

mkpankov commented Apr 23, 2018

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?

Full project source.

@dtolnay
Copy link
Member

dtolnay commented Apr 23, 2018

Thanks! This was a bug. I published 1.0.43 with a fix and your test should work as written.

@dtolnay dtolnay transferred this issue from serde-rs/serde Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants