actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Query extractor does not work well with enum

Open juchiast opened this issue 9 months ago • 0 comments

Steps to Reproduce (for bugs)

I have 2 kinds of query, using this enum:

        #[derive(Deserialize, Debug)]
        #[serde(untagged)]
        pub enum Query {
            FlowTag { flow: i32, tag: String },
            Id { id: String },
        }

However, it fails to decode ?flow=123&tag=latest. Replacing flow type with String makes it pass.

        #[derive(Deserialize, Debug)]
        #[serde(untagged)]
        pub enum Query1 {
            // fail when using i32
            FlowTag { flow: i32, tag: String },
            Id { id: String },
        }

        #[derive(Deserialize, Debug)]
        #[serde(untagged)]
        pub enum Query2 {
            // success when using a string
            FlowTag { flow: String, tag: String },
            Id { id: String },
        }

        let req = TestRequest::with_uri("/start?flow=123&tag=latest").to_http_request();
        web::Query::<Query1>::extract(&req).await.unwrap();
        web::Query::<Query2>::extract(&req).await.unwrap();

juchiast avatar Feb 04 '25 16:02 juchiast