Skip to content

Serde panics when flattening struct and Value #1379

@chrisduerr

Description

@chrisduerr

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions