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

Failing to deserialize skipped default None variant for content generated by rmp_serde::to_vec #301

Open
jsprog opened this issue Mar 23, 2022 · 0 comments

Comments

@jsprog
Copy link

jsprog commented Mar 23, 2022

To trigger this issue, you need to create a struct containing at least two fields:

  • the first field is optional with the default value of None, and the created instance contains the None variant for this field.
  • the optional field is not the last one in the structure.
  • try to deserialize some content serialized with rmp::to_vec (not rmp::to_vec_named).
use serde::{Serialize, Deserialize};

fn main() {
    let record = Record::default();
    println!("{:?}", record);

    let encoded = rmp_serde::to_vec(&record).unwrap(); // this will fail later with decoding
    // let encoded = rmp_serde::to_vec_named(&record).unwrap(); // note: this won't fail with decoding
    println!("{:?}", encoded);

    let decoded: Record = rmp_serde::from_slice(&encoded).unwrap();
    println!("{:?}", decoded);
}


#[derive(Serialize, Deserialize, Debug)]
struct Record {
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    optional_field: Option<u8>, // note: this isn't the last field

    another_field: u8, // note: you need to place this after 'optional_field' to trigger this issue
}
impl Default for Record {
    fn default() -> Self {
        Self {
            optional_field: None, // note: replacing with Some(value) won't trigger this issue
            another_field: 10,
        }
    }
}

Testing Platform:

  • Linux Mint 20.2 (x86-64)
  • cargo 1.58.0
  • rustc 1.58.1
  • serde = "1.0.136"
  • rmp-serde = "1.0.0"
@jsprog jsprog changed the title rmp_serde::to_vec is failing to deserialize skipped default None variant Failing to deserialize skipped default None variant for content generated by rmp_serde::to_vec Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant