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

BUG: Tagged enum serialization => deserialization produces error with certain crate layout #768

Closed
folex opened this issue Apr 6, 2021 · 2 comments

Comments

@folex
Copy link

folex commented Apr 6, 2021

Hi! First, thank for your amazing work. Serde is a gem!

rustup info

active toolchain
----------------
nightly-x86_64-apple-darwin (default)
rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)

Affected versions:

serde: 1.0.118 - 1.0.125
serde_json:: 1.0.57 - 1.0.64

Minimal example

I've stumbled upon a pretty strange issue. I managed to minify the example to 3 dependencies https://github.com/folex/serde_bug_report and minimal lines of code in tests/test.rs.

What happened

I have a tagged enum. I serialize it, then deserialize, and get an error

Error("invalid type: map, expected u64", line: 0, column: 0)

The code is as follows. Define struct, tagged enum wrapper, serialize it to bytes via serde_json, and then deserialize. Pretty usual deal. There's just one strange detail to it, see after the code snippet.

use serde::{Deserialize, Serialize};
use serde_json

#[test]
fn deserialize() {
    let msg = FooMessage::Particle(<_>::default());
    let bytes = serde_json::to_vec(&msg).unwrap();
    let test_msg: Result<FooMessage, _> = serde_json::from_slice(&bytes);
    println!("{:?}", test_msg);
    test_msg.unwrap();
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
pub struct FooParticle {
    pub timestamp: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(tag = "action")]
pub enum FooMessage {
    Particle(FooParticle),
}

Stranger things

For the error to happen, 2 things must take place:

  1. tracing-bunyan-formatter should be in the dependencies
  2. one of the fields must be named timestamp

Cargo.toml:

[dependencies]
serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.64"
tracing-bunyan-formatter = "0.2.0"

I couldn't figure out why on earth this tracing dependency breaks things, but for me it certainly does. I can avoid using this crate, but maybe this is a symptom of a bigger issue? It was pretty hard to debug and I'd appreciate if you have any suggestions and ideas on the possible reasons.

Thanks!

@thekuom
Copy link

thekuom commented May 2, 2021

I am also seeing this issue albeit with a different example. My minimal example is here: https://github.com/thekuom/serde_json_tracing_bunyan_formatter_bug_report and fails while using #[serde(flatten)]

@thekuom
Copy link

thekuom commented May 2, 2021

@folex, I think this is a result of #505

You should be able to fix this by changing your deps to

tracing-bunyan-formatter = { version = "0.2.0", default-features = false }

or using the workaround mentioned here: #505 (comment)

tracing-bunyan-formatter has the arbitrary-precision feature turned on by default

@dtolnay dtolnay closed this as completed Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants