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

Can't deserialize some cases when using #[serde(flatten)] #14

Closed
kardeiz opened this issue Jul 5, 2018 · 4 comments
Closed

Can't deserialize some cases when using #[serde(flatten)] #14

kardeiz opened this issue Jul 5, 2018 · 4 comments

Comments

@kardeiz
Copy link
Contributor

kardeiz commented Jul 5, 2018

Hi! When using serde's relatively new #[serde(flatten)] feature, nested values are sometimes not parsed correctly. It seems like values that have to be parsed from strings (e.g., numbers) are failing; it seems like they are deserialized too early into a string, rather than the final value type.

For example, here is a failing test

#[test]
fn flattened_struct() {
    #[derive(Debug,Serialize,Deserialize,PartialEq)]
    struct InnerQuery {
        name: String,
        age: u32
    }

    #[derive(Debug,Serialize,Deserialize,PartialEq)]
    struct Query {
        #[serde(flatten)]
        inner: InnerQuery,
    }

    let query = Query {
        inner: InnerQuery {
            name: "Carrot".into(),
            age: 45
        }
    };

    let params = "name=Carrot&age=45";

    let rec_params: Query = qs::from_str(params).unwrap();

    assert_eq!(rec_params, query);
}

This panics with "invalid type: string \"45\", expected u32".

Have you encountered this? Any thoughts or workarounds?

@kardeiz
Copy link
Contributor Author

kardeiz commented Jul 5, 2018

Nevermind, this appears to be a problem higher up: serde-rs/serde#1183 and nox/serde_urlencoded#33. Closing.

@kardeiz kardeiz closed this as completed Jul 5, 2018
@samscott89
Copy link
Owner

Happened to come across this again today, and proposed this workaround. Leaving here for future interest.

@maxcountryman
Copy link

Happened to come across this again today, and proposed this workaround. Leaving here for future interest.

Very helpful--thank you! I wonder if it isn't better to suggest the serde_with crate as an alternative to rolling your own fromstr?

@samscott89
Copy link
Owner

Happened to come across this again today, and proposed this workaround. Leaving here for future interest.

Very helpful--thank you! I wonder if it isn't better to suggest the serde_with crate as an alternative to rolling your own fromstr?

Thanks for the suggestion, yes that looks like a better approach!

@samscott89 samscott89 pinned this issue Oct 1, 2021
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

3 participants