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

Using flatten emits invalid RON #526

Closed
porkbrain opened this issue Mar 5, 2024 · 1 comment · Fixed by #527
Closed

Using flatten emits invalid RON #526

porkbrain opened this issue Mar 5, 2024 · 1 comment · Fixed by #527

Comments

@porkbrain
Copy link
Contributor

porkbrain commented Mar 5, 2024

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.

@juntyr
Copy link
Member

juntyr commented Mar 5, 2024

@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 :)

juntyr added a commit to juntyr/ron that referenced this issue Mar 5, 2024
Co-authored-by: porkbrain <git@porkbrain.com>
juntyr added a commit that referenced this issue Mar 5, 2024
* Add #526 as a test

Co-authored-by: porkbrain <git@porkbrain.com>

* Fix indemap deprecated API usage

---------

Co-authored-by: porkbrain <git@porkbrain.com>
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

Successfully merging a pull request may close this issue.

2 participants