ron-rs/ron

Using `flatten` emits invalid RON

Closed this issue · 1 comments

When I use #[serde(flatten)], serialization emits invalid RON. Here's a minimal example of the issue I am facing.

# Cargo.toml

[package]
name = "ron_flatten_bug"
version = "0.1.0"
edition = "2021"

[dependencies]
ron = "=0.8.1"
serde = { version = "=1.0.197", features = ["derive"] }
// src/main.rs

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct SomeCollection {
    inner: Vec<SomeItem>,
}

#[derive(Serialize, Deserialize)]
struct SomeItem {
    #[serde(flatten)]
    foo: Foo,
    #[serde(flatten)]
    bar: Bar,
}

#[derive(Serialize, Deserialize)]
struct Bar {
    name: String,
    some_enum: Option<SomeEnum>,
}

#[derive(Serialize, Deserialize)]
struct Foo {
    something: String,
}

#[derive(Serialize, Deserialize)]
enum SomeEnum {
    A,
    B,
}

fn main() {
    let scene = SomeCollection {
        inner: vec![SomeItem {
            foo: Foo {
                something: "something".to_string(),
            },
            bar: Bar {
                name: "name".to_string(),
                some_enum: Some(SomeEnum::A),
            },
        }],
    };

    let raw = ron::ser::to_string(&scene).unwrap();
    let _deser_scene: SomeCollection = ron::de::from_str(&raw).unwrap(); // panics

    let raw = ron::ser::to_string_pretty(&scene, Default::default()).unwrap();
    let _deser_scene: SomeCollection = ron::de::from_str(&raw).unwrap(); // panics
}

EDIT: Just read #496. Wish all the best 🫀 Perhaps the example above will be useful somehow.

@porkbrain Thanks for the test case (and the edit)! I can confirm that it works on main and I'll push your case to the repo as a test case so it will work in v0.9 :)