tokio-rs / axum

Ergonomic and modular web framework built with Tokio, Tower, and Hyper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

axum_extra Query doesn't support Vec<Enum>

yuki0418 opened this issue · comments

  • I have looked for existing issues (including closed) about this

Bug Report

Similar issue with #434 but in Enum case.
Query for Vec<Enum> returns error with Failed to deserialize query string: invalid type: string "Pending,Cancelled", expected a sequence.

Version

  • axum v0.7.4
  • axum-extra v0.9.3

Platform

macOs v14.2.1

Crates

axum_extra::extract::Query

Description

I tried get array of status defined in Enum from url. When I use Vec<OrderStatus>, OrderStatus is Enum and Query seems not support to deserialize it.

URL: ?status=Pending,Cancelled

// I use this Enum with diesel as well
#[derive(Debug, AsExpression, FromSqlRow, PartialEq, Deserialize, Serialize, Clone)]
#[diesel(sql_type = diesel::sql_types::Tinyint)]
pub enum OrderStatus {
    Cancelled = 0,
    Pending = 1,
}

This not work

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetQuery {
    pub status: Option<Vec<OrderStatus>>,
}

// Error with 400 Bad Request: Failed to deserialize query string: invalid type: string "Pending,Cancelled", expected a sequence

But when String works

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetQuery {
    pub status: Option<Vec<String>>,
}

// GetQuery { status: Some(["Pending,Cancelled"]) }

Single Enum value works

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetQuery {
    pub status: Option<OrderStatus>,
}

// GetQuery { status: Some(Pending) }

Always thank you for your helping!!

Comma-separated values is not how Vec fields are (de)serialized with serde_html_form. See the examples in its documentation.

Also if you are having issues with that extractor next time, I would recommend trying to reproduce the problem with serde_html_form independently of axum, and opening a GitHub discussion there.

@jplatte
Thanks so much!!
And sorry for I missed the example and creating the issue rather than in discussion.
I will follow your advice for next time!!