utoipa icon indicating copy to clipboard operation
utoipa copied to clipboard

IntoParams does not respect `#[serde(flatten)]`

Open jelacious opened this issue 1 year ago • 3 comments

Hiya!

I am implementing some pagination for a few of my axum endpoints and am not able to properly get the Pagination fields into my structs. In my twisted mind it'd make sense for the IntoParams derive macro to respect #[serde(flatten)] to inline all fields from the flattened structure into the other.

Here's an example of what I mean using utoipa-swagger-ui:

image

I'd expect both page and per_page to be included into the parameters as normal queries instead of a json object.

Relevant code:

#[derive(Serialize, Deserialize, Debug, IntoParams)]
#[into_params(style = Form, parameter_in = Query)]
pub struct DeviceQuery {
    /// Device names to search for.
    #[serde(default)]
    #[param(style = Form, explode, allow_reserved)]
    name: Vec<String>,
    /// Profile names to search for.
    #[serde(default)]
    #[param(allow_reserved)]
    r#type: Option<String>,
    /// Pagination desired.
    #[serde(flatten, default)]
    #[param(inline)] // this has to be included or Pagination isn't found 
    pagination: Option<Pagination>,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, ToSchema)] // should I be deriving IntoParams (doesn't do anything in my experience)?
#[serde_as]
#[serde(default)]
pub struct Pagination {
    /// Which "page" of the results desired.
    #[serde_as(as = "DisplayFromStr")]
    page: usize,
    /// Number of results per page.
    #[serde_as(as = "DisplayFromStr")]
    per_page: usize,
}

impl Default for Pagination {
    fn default() -> Self {
        Self {
            page: 0,
            per_page: 50,
        }
    }
}

Am I missing something. or is this working as intended?

jelacious avatar Jan 10 '24 14:01 jelacious

Adding IntoParams or ToSchema on both has no effect. Using #[param(inline)] or #[schema(inline)] or both has no effect. I am unsure what inline is even supposed to do at this point.

jelacious avatar Jan 22 '24 16:01 jelacious

+1 I'm currently in the same situation as of version 4.2.0

KeeganMyers avatar Mar 19 '24 19:03 KeeganMyers