3Hren / 3Hren/msgpack-rust

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

Open
#301 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.4k
Forks
162
PR merge metrics
No merged PRs in 30d

Description

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`).

```rs
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, // 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"

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.