utoipa icon indicating copy to clipboard operation
utoipa copied to clipboard

Keep serde skipped field in API Spec

Open VirusBLITZ opened this issue 1 year ago • 2 comments

Say I have a struct like

#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
pub struct Client {
    #[serde(skip_deserializing)]
    pub test: Option<bool>,
}

the field test is being ignored because of the serde skip_deserializing macro, is there a way to keep it in the spec?

VirusBLITZ avatar Feb 15 '24 22:02 VirusBLITZ

I agree it would be very nice to have some way to not skip these fields under some circumstances. One workaround is to create another struct with the unskipped field and the original struct as properties and use #[serde(flatten)]. From a project I'm working on (also doing the same with sqlx in this case):

#[derive(FromRow, Serialize, Deserialize, ToSchema, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Tag {
    #[serde(skip_deserializing)]
    pub id: Uuid,
    // ...rest of struct fields
}

#[derive(FromRow, Serialize, Deserialize, ToSchema, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TagWithId {
    pub id: Uuid,
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub tag: Tag,
}

hutchisr avatar Jun 03 '24 21:06 hutchisr

I agree it would be very nice to have some way to not skip these fields under some circumstances. One workaround is to create another struct with the unskipped field and the original struct as properties and use #[serde(flatten)]. From a project I'm working on (also doing the same with sqlx in this case):

#[derive(FromRow, Serialize, Deserialize, ToSchema, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Tag {
    #[serde(skip_deserializing)]
    pub id: Uuid,
    // ...rest of struct fields
}

#[derive(FromRow, Serialize, Deserialize, ToSchema, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TagWithId {
    pub id: Uuid,
    #[sqlx(flatten)]
    #[serde(flatten)]
    pub tag: Tag,
}

Thanks for the reply, seems like a solution that I can live with 😄

VirusBLITZ avatar Jun 03 '24 21:06 VirusBLITZ