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 #389

Closed
wcarmon opened this issue Dec 11, 2022 · 2 comments
Closed

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

wcarmon opened this issue Dec 11, 2022 · 2 comments

Comments

@wcarmon
Copy link

wcarmon commented Dec 11, 2022

The code below demonstrates that flatten and alias features of serde don't work in the toml library.

Env

  • lib version: toml = "0.5.9"
  • 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() {
        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() {
        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);
    }
}
@epage
Copy link
Member

epage commented Dec 12, 2022

Have you tried reproducing this with other serde formats? I have a feeling that alias is more on the serde_derive side than on the serde format side.

@wcarmon
Copy link
Author

wcarmon commented Dec 15, 2022

you are correct.

It it happens in serde_json too

use serde::Deserialize;

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

        i: i64,
    }

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

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

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

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

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

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

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

@wcarmon wcarmon closed this as not planned Won't fix, can't repro, duplicate, stale Dec 15, 2022
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

2 participants