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

Marking a struct field as flattened makes the struct ignore its tag #1468

Closed
jwillbold opened this issue Jan 30, 2019 · 2 comments
Closed

Marking a struct field as flattened makes the struct ignore its tag #1468

jwillbold opened this issue Jan 30, 2019 · 2 comments
Labels

Comments

@jwillbold
Copy link
Contributor

If I define a struct with a tag and add a field with the "flatten" attribute to this struct. The structs's tag seems to be ignored.

extern crate serde; // 1.0.84
#[macro_use]
extern crate serde_derive; // 1.0.84
#[macro_use]
extern crate serde_json; // 1.0.34


#[derive(Serialize, Debug)]
#[serde(tag="type")]
pub struct Program {
    #[serde(flatten)]
    pub body: ProgramType
}

#[derive(Serialize, Debug)]
#[serde(tag="sourceType", content="body")]
pub enum ProgramType {
    #[serde(rename="script")]
    Script(u64),
}


fn main() {
    let p = Program{body: ProgramType::Script(0)};
    // This assert fails, output:
    //  left: `Object({"body": Number(0), "sourceType": String("script")})`,
    //  right: `Object({"body": Number(0), "sourceType": String("script"), "type": String("Program")})`
    assert_eq!(serde_json::to_value(p).unwrap(),
                json!({"type": "Program", "sourceType": "script", "body": 0}))
}
@dtolnay dtolnay added the bug label Jan 30, 2019
@dtolnay
Copy link
Member

dtolnay commented Jan 30, 2019

Thanks! This looks like a bug. Would you be interested in sending a PR to fix it?

@jwillbold
Copy link
Contributor Author

Sure. I implemented the tagged structs in the first place so I guess it's only fair if I have to do the bug fixing :D But it might take a few days.

@dtolnay dtolnay closed this as completed in 1d6ef76 Feb 3, 2019
dtolnay added a commit that referenced this issue Feb 3, 2019
Fixed #1468, flattened struct fields made structs ignore their tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants