Skip to content

#[serde_as(as = Option<...>)] does not allow field to be missing #183

Answered by jonasbb
chyvonomys asked this question in Q&A
Discussion options

You must be logged in to vote

This unfortunately a side effect of using deserialize_with. I see how I can better document the pitfall.

If you provide serde's default attribute, then it behaves as expected. It will deserialize a None in this case:

#[serde_as]
#[derive(Deserialize, Debug)]
struct Query {
    search: Option<String>,
    #[serde_as(as = "Option<StringWithSeparator::<CommaSeparator, u32>>")]
    #[serde(default)]
    priority: Option<Vec<u32>>,
}

If you do not care about the distinction between priority was provided but the list is empty and priority is missing, you can also remove the Option from priority:

#[serde_as(as = "StringWithSeparator::<CommaSeparator, u32>")]
#[serde(default, skip_serializing_if = 

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by jonasbb
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #183 on December 16, 2020 20:20.