Serde: Serde panics when flattening struct and Value

Created on 9 Sep 2018  路  1Comment  路  Source: serde-rs/serde

When trying to deserialize a struct which contains another struct with the #[serde(flatten)] annotation and a field with the #[serde(flatten)] annotation and the type toml::Value (same with serde_yaml or serde_json), serde will panic.

A minimal example for reproducing this:

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate toml;

#[derive(Debug, Deserialize)]
struct Test {
    text: Option<String>,
    #[serde(flatten)]
    settings: Settings,
    #[serde(flatten)]
    extra: toml::Value,
}

#[derive(Debug, Deserialize)]
struct Settings {
    width: Option<u8>,
}

fn main() {
    let file = "text = \"test\"\nwidth = 8";
    let test: Test = toml::from_str(file).unwrap();
}

playground

This will result in the following error:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value'
bug

Most helpful comment

Fixed in serde 1.0.78.

>All comments

Fixed in serde 1.0.78.

Was this page helpful?
0 / 5 - 0 ratings