Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to parse flatten query #2698

Closed
1 task done
luisfbl opened this issue Apr 4, 2024 · 1 comment
Closed
1 task done

Failed to parse flatten query #2698

luisfbl opened this issue Apr 4, 2024 · 1 comment

Comments

@luisfbl
Copy link

luisfbl commented Apr 4, 2024

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

Bug Report

Version

axum v0.7.4

Platform

Linux gentoo 6.1.69-gentoo-dist #1 SMP PREEMPT_DYNAMIC Thu Jan 18 08:26:20 -04 2024 x86_64 AMD Ryzen 5 1600 Six-Core Processor AuthenticAMD GNU/Linux

Crates

axum query

Description

For some reason, when deserializing a query for a struct that has a flatten attribute field, the deserialization does not occur as expected

#[derive(Serialize, Deserialize, Validate)]
pub struct FilterProducts {
    #[garde(custom(validate_price))]
    pub max_price: Option<Decimal>,
    #[garde(custom(validate_price))]
    pub min_price: Option<Decimal>,
    #[garde(skip)]
    pub min_stars: Option<StarRating>,
    #[garde(range(min = 1, max = 100))]
    pub product_type: Option<i32>,
    #[garde(skip)]
    pub start_time: Option<time::Time>,
    #[garde(skip)]
    pub day_of_week: Option<WeekDay>,
    #[garde(dive)]
    #[serde(flatten)]
    pub pagination: Pagination,
    #[garde(dive)]
    #[serde(flatten)]
    pub location: Option<Location>
}

#[derive(Serialize, Deserialize, Validate)]
pub struct Pagination {
    #[garde(range(min = 1, max = 100))]
    pub page: i16,
    #[garde(range(min = 5, max = 100))]
    pub per_page: i16
}

#[derive(Serialize, Deserialize, Validate)]
pub struct Location {
    #[garde(range(min = -90.0000000, max= 90.0000000))]
    pub latitude: f64,
    #[garde(range(min = -180.0000000, max= 180.0000000))]
    pub longitude: f64,
}

My query param:

WithValidation(query): WithValidation<Query<FilterProducts>>,

When I run test (same error happens when I try to access from browser)

let query = FilterProducts {
    max_price: None,
    min_price: None,
    min_stars: None,
    product_type: None,
    start_time: None,
    day_of_week: None,
    pagination: Pagination {
        page: 1,
        per_page: 10,
     },
    location: None,
};

let res = server.get("/api/v1/products")
        .add_query_params(query)
        .await;

println!("{}", res.text())

or

http://localhost:5443/api/v1/products?page=1&per_page=10

The error:

Failed to deserialize query string: invalid type: string "1", expected i16

Maybe the error isn't with axum, or maybe I'm doing something wrong, I hope anyone can help me

@jplatte
Copy link
Member

jplatte commented Apr 4, 2024

This is serde-rs/serde#1183.

@jplatte jplatte closed this as not planned Won't fix, can't repro, duplicate, stale Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants