Serde #[serde(skip_serializing_if="Option::is_none")] fails
- Dominant language
- Rust
- Stars
- 1.4k
- Forks
- 162
- PR merge metrics
- No merged PRs in 30d
Description
When using #[serde(skip_serializing_if="Option::is_none")] on a field that has the value None and if it is not the last field deserialization fails(maybe it is already serialized in a wrong way)
Here is some test code:
```
#[cfg(test)]
mod test_rmp_serde {
use serde::{Serialize, Deserialize};
use serde_msgpack::{Serializer, Deserializer};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Example1 {
test_field: i64,
name: Option,
items: Vec,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Example2 {
#[serde(rename = "test_field_renamed")]
test_field: i64,
// If you remove this annotation, it will pass
#[serde(skip_serializing_if="Option::is_none")]
name: Option,
#[serde(rename = "items_renamed")]
items: Vec,
}
#[test]
fn without_serde_attributes() {
let original = Example1 {
test_field: 0,
items: vec![],
name: None,
};
let mut buf = Vec::new();
{
let mut ser = Serializer::new(&mut buf);
original.serialize(&mut ser).unwrap();
}
let mut deser = Deserializer::new(buf.as_slice());
let deserialized = Example1::deserialize(&mut deser).unwrap();
assert_eq!(original, deserialized);
}
#[test]
fn with_serde_attributes() {
let original = Example2 {
test_field: 0,
items: vec![],
name: None,
};
let mut buf = Vec::new();
{
let mut ser = Serializer::new(&mut buf);
original.serialize(&mut ser).unwrap();
}
let mut deser = Deserializer::new(buf.as_slice());
let deserialized = Example2::deserialize(&mut deser).unwrap();
assert_eq!(original, deserialized);
}
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.