utoipa
utoipa copied to clipboard
Keep serde skipped field in API Spec
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?
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,
}
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 😄