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

extract::Query does not work with #[serde(flatten)] #1753

Closed
1 task done
Mivik opened this issue Feb 13, 2023 · 2 comments
Closed
1 task done

extract::Query does not work with #[serde(flatten)] #1753

Mivik opened this issue Feb 13, 2023 · 2 comments

Comments

@Mivik
Copy link

Mivik commented Feb 13, 2023

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

Bug Report

Version

├── axum v0.6.6
│   ├── axum-core v0.3.2

Platform

Ubuntu 22.04.1 LTS (x86_64)

Description

Using #[serde(flatten)] inside the Query parameter leads to the failure to deserialize the parameter, if fields inside the flattened struct are not String.

extern crate serde;
extern crate axum;
extern crate tokio;

use axum::extract::Query;
use serde::Deserialize;
use std::net::SocketAddr;

#[derive(Deserialize)]
struct Params1 {
    page: u32,
}

#[derive(Deserialize)]
struct InnerParam {
    page: u32,
}

#[derive(Deserialize)]
struct Params2 {
    #[serde(flatten)]
    inner: InnerParam,
}

#[tokio::main]
async fn main() {
    use axum::routing::*;
    let app = Router::new()
        .route("/p1", get(p1))
        .route("/p2", get(p2));

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap()
}


async fn p1(Query(param): Query<Params1>) -> String {
    format!("page is {}", param.page)
}

async fn p2(Query(param): Query<Params2>) -> String {
    format!("page is {}", param.inner.page)
}

Theoretically, p1 and p2 should behaves exactly the same. However the result denies that:

> curl 'http://127.0.0.1:3000/p1?page=42'
> page is 42
> curl 'http://127.0.0.1:3000/p2?page=42'
> Failed to deserialize query string: invalid type: string "42", expected u32
@Mivik
Copy link
Author

Mivik commented Feb 13, 2023

After investigation this seems to be a bug of serde_urlencoded (nox/serde_urlencoded#33).

@davidpdrsn
Copy link
Member

Yep. Nothing axum can do about this.

@davidpdrsn davidpdrsn closed this as not planned Won't fix, can't repro, duplicate, stale Feb 13, 2023
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