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

Bug: alias and flatten don't work together #2341

Closed
wcarmon opened this issue Dec 15, 2022 · 3 comments
Closed

Bug: alias and flatten don't work together #2341

wcarmon opened this issue Dec 15, 2022 · 3 comments

Comments

@wcarmon
Copy link

wcarmon commented Dec 15, 2022

The code below demonstrates that flatten and alias features of serde don't work together.

Below shows examples from both serde_json and toml library.

Env

  • lib version: toml = "0.5.9"
  • lib version: serde_json = "1.0.89"
  • cargo version: cargo 1.67.0-nightly (ba607b23d 2022-11-22)
  • serde version serde = { version = "1.0.147", features = ["derive"] }
  • serde flattening feature
  • serde alias feature

Here is an MWE which demonstrates the bug

use serde::Deserialize;

#[derive(Deserialize)]
struct Inner {
    #[serde(alias = "myBool")]
    b: bool,

    i: i64,
}

#[derive(Deserialize)]
struct Outer {
    #[serde(flatten)]
    a: Inner,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn works_as_expected_in_toml() {
        let t_str = r#"
b = true
i = 7
"#;

        let res: Result<Outer, toml::de::Error> = toml::from_str(t_str);
        let got = res.unwrap();

        assert_eq!(true, got.a.b);
        assert_eq!(7, got.a.i);
    }

    #[test]
    fn shows_bug_in_toml() {
        let t_str = r#"
myBool = true
i = 7
"#;

        let res: Result<Outer, toml::de::Error> = toml::from_str(t_str);
        let got = res.unwrap(); // *** incorrect behavior manifests here

        assert_eq!(true, got.a.b);
        assert_eq!(7, got.a.i);
    }

    #[test]
    fn works_as_expected_in_json() {
        let j_str = r#"
{
    "b": true,
    "i": 7
}
"#;

        let res = serde_json::from_str::<Outer>(j_str);
        let got = res.unwrap();

        assert_eq!(true, got.a.b);
        assert_eq!(7, got.a.i);
    }

    #[test]
    fn shows_bug_in_json() {
        let j_str = r#"
{
    "myBool": true,
    "i": 7
}
"#;

        let res = serde_json::from_str::<Outer>(j_str);
        let got = res.unwrap(); // *** incorrect behavior manifests here

        assert_eq!(true, got.a.b);
        assert_eq!(7, got.a.i);
    }
}

Related

@jonasbb
Copy link
Contributor

jonasbb commented Dec 15, 2022

This is a known problem: #1504.

@Mingun
Copy link
Contributor

Mingun commented May 9, 2023

This was fixed (partially) by #2387, the in-place part is fixed in #2443

@dtolnay
Copy link
Member

dtolnay commented Jul 9, 2023

Closing as a duplicate of #2188.

@dtolnay dtolnay closed this as completed Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants