Skip to content

Roundtrip failure when using flatten and newtype together #555

@mxgrey

Description

@mxgrey

I'm working off of the master branch at commit 7466647 attempting to take advantage of the ability to parse maps into structs so that I can use #[serde(flatten)]. It almost works, but I get a strange parsing error during a roundtrip for a particular structure. Here's a minimal reproducible example:

use serde::{Serialize, Deserialize};
use ron::ser::*;

fn main() {
    let data = Outer {
        inner: Inner { new_type: NewType(vec![]) },
    };

    let serialized = to_string_pretty(&data, PrettyConfig::default()).unwrap();
    println!("{serialized}");
    let deserialized: Outer = ron::from_str(&serialized).unwrap();
}

#[derive(Serialize, Deserialize)]
struct Outer {
    #[serde(flatten)] // Without this attribute, everything works
    inner: Inner,
}

#[derive(Serialize, Deserialize)]
struct Inner {
    new_type: NewType,
}

#[derive(Serialize, Deserialize)]
// #[serde(transparent)] // With this attribute, everything works
struct NewType(Vec<u32>);

The error I get is

SpannedError { code: InvalidValueForType { expected: "u32", found: "a sequence" }, position: Position { line: 3, col: 1 } }

Strangely if I add #[serde(transparent)] to NewType or if I remove #[serde(flatten)] from Outer then everything works. In my specific case, I definitely don't mind using #[serde(transparent)], but it seems like the error is probably a bug that's worth addressing.

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