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

Order of parameters matter when extracting Request<Body> and Extension<..> #1169

Closed
qqwa opened this issue Jul 17, 2022 · 2 comments
Closed

Comments

@qqwa
Copy link

qqwa commented Jul 17, 2022

Bug Report

Version

├── axum v0.5.10 (https://github.com/tokio-rs/axum.git#329bd5f9)
│ ├── axum-core v0.2.6 (https://github.com/tokio-rs/axum.git#329bd5f9)

and

├── axum v0.5.13
│ ├── axum-core v0.2.7

Platform

Linux host 5.18.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 25 Jun 2022 20:22:01 +0000 x86_64 GNU/Linux

Description

I would expect the order in which I define the parameters not to matter. If I extract Request<Body> before any Extension<..> it will just not find them at runtime, see example. But if I just define the Extension<..> first it works as expected. I don't know if there is the same issue for other extractors. But at least for the Query<..> it doesn't matter if it is before or after the Request<Body>.

Example:

use axum::{routing::get, Extension, Router, http::Request, body::Body};
use std::{
    net::SocketAddr,
    sync::Arc,
};
struct State;

async fn good(_state: Extension<Arc<State>>, _req: Request<Body>) {}
async fn bad(_req: Request<Body>, _state: Extension<Arc<State>>) {}

#[tokio::main]
async fn main() {
    let state = Arc::new(State );
    let app = Router::new()
        .route("/good", get(good))
        .route("/bad", get(bad))
        .layer(Extension(state));
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}
$ curl localhost:3000/good
$ curl localhost:3000/bad
Missing request extension: Extension of type `alloc::sync::Arc<minimal::State>` was not found. Perhaps you forgot to add it? See `axum::Extension`.
@davidpdrsn
Copy link
Member

We know 😞 and it's something we are hoping to improve in 0.6. See #1115 and #1121

It's documented here.

I'll close this as we already have other issues about this.

@qqwa
Copy link
Author

qqwa commented Jul 17, 2022

Ah so it is documented, I guess my bad for not finding it. Would have expected to find a note of it here: https://docs.rs/axum/latest/axum/struct.Extension.html
But adding it now isn't really necessary if it will be fixed soon, and it should be hopefully easy enough to find through github issues now, I guess.
Thanks for the reply tho!

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