Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roundtrip failure when using flatten and newtype together #555

Open
mxgrey opened this issue Jan 20, 2025 · 0 comments
Open

Roundtrip failure when using flatten and newtype together #555

mxgrey opened this issue Jan 20, 2025 · 0 comments

Comments

@mxgrey
Copy link

mxgrey commented Jan 20, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant