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

Sequences with 1 entry do not work if nested with #[serde(flatten)] #25

Closed
liss-h opened this issue Jan 29, 2025 · 1 comment
Closed

Comments

@liss-h
Copy link

liss-h commented Jan 29, 2025

Hi,
there seems to be an issue with the #[serde(flatten)] attribute, as it prevents sequences with 1 element from being properly parsed. (zero or more than one work fine)

Example:

use serde::Deserialize;

#[derive(Deserialize)]
struct Seq {
    #[serde(default)]
    seq: Vec<String>,
}

#[derive(Deserialize)]
struct WithFlatten {
    #[serde(flatten)]
    inner: Seq,
}

fn main() {
    { // this works as expected
        let just_seq: Seq = serde_html_form::from_str("seq=1").unwrap();
        assert_eq!(just_seq.seq, vec!["1".to_owned()]);
    }

    { // this fails on the unwrap()

         // called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"1\", expected a sequence")
        let with_flatten: WithFlatten = serde_html_form::from_str("seq=1").unwrap(); 
        assert_eq!(with_flatten.inner.seq, vec!["1".to_owned()]);
    }
}
@jplatte
Copy link
Owner

jplatte commented Jan 29, 2025

This is an instance of serde-rs/serde#1183 and can't be fixed in thie library.

I can only strongly recommend to not use serde(flatten) when you can help it. Even when it works, it degrades performance quite a lot compared to the equivalent solution without flatten.

@jplatte jplatte closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2025
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