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

Attributes alias and flatten don't seem to work together (serde_json) #1976

Closed
Ploppz opened this issue Feb 4, 2021 · 3 comments
Closed

Comments

@Ploppz
Copy link

Ploppz commented Feb 4, 2021

At least in this example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c2ee5b544b5b939cf67f364d756f5bea

serde_json::from_str gives error

"missing field calculation_status"

even though calculationStatus is there and we have such an alias.

If you move the calculation_status field with attributes directly into the Plate struct (and remove PlateFlags), it works.

@bmc-msft
Copy link

bmc-msft commented Apr 5, 2021

I can confirm this is an issue.

Example:

use serde_derive::{Deserialize};

#[derive(Deserialize, Debug)]
pub struct Config {
    #[serde(alias = "field1")]
    pub new_name: Option<String>,

    pub field2: Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct NestedConfig {
    #[serde(flatten)]
    pub common: Config,
}

fn main() {
    let raw = "{\"field1\": \"value1\", \"field2\": \"value2\"}";
    let config: Config = serde_json::from_str(&raw).unwrap();
    println!("{:?}", config);

    let nested: NestedConfig = serde_json::from_str(&raw).unwrap();
    println!("{:?}", nested);
}

This results in:

Config { new_name: Some("value1"), field2: Some("value2") }
NestedConfig { common: Config { new_name: None, field2: Some("value2") } }

@SciStarter
Copy link

This looks like a duplicate of #1504

@dtolnay
Copy link
Member

dtolnay commented Jan 23, 2022

Closing as a duplicate of #1504.

@dtolnay dtolnay closed this as completed Jan 23, 2022
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