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

Option<Path<T>> extractor does not return None #3208

Open
demhadais opened this issue Feb 2, 2025 · 2 comments
Open

Option<Path<T>> extractor does not return None #3208

demhadais opened this issue Feb 2, 2025 · 2 comments

Comments

@demhadais
Copy link

demhadais commented Feb 2, 2025

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

Bug Report

Platform

Darwin Ahmeds-MacBook-Air.local 24.2.0 Darwin Kernel Version 24.2.0: Fri Dec 6 18:51:28 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T8112 arm64

Description

I tried for a long time to get this to work, and I'm fairly confident I'm misunderstanding how this extractor is meant to be used and/or the expected result. Anyways, the issue is that with the following code, theOption<Path<T>> extractor doesn't actually ever return the None variant.

use axum::{extract::Path, routing::get, Router};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/{name}", get(handler));
    let listener = TcpListener::bind("localhost:8080").await.unwrap();

    axum::serve(listener, app).await.unwrap();
}

async fn handler(name: Option<Path<String>>) -> String {
    match name {
        Some(Path(name)) => format!("{name}\n"),
        None => "hello\n".to_string(),
    }
}

Here are some snippets from my command-line:
No path - I expect this to error

$ curl -i http://localhost:8080
HTTP/1.1 404 Not Found
content-length: 0
date: Sun, 02 Feb 2025 04:37:43 GMT

Just a slash - I expect this to show hello\n, but instead it errors too

$ curl -i http://localhost:8080/
HTTP/1.1 404 Not Found
content-length: 0
date: Sun, 02 Feb 2025 04:37:43 GMT

A path parameter - This works

$ curl -i http://localhost:8080/some-random-string
HTTP/1.1 200 OK
content-type: text/plain; charset=utf-8
content-length: 19
date: Sun, 02 Feb 2025 04:43:03 GMT

some-random-string

Version

├── axum v0.8.1
│   ├── axum-core v0.5.0
@demhadais demhadais changed the title Option<Path<T>> extractor does not act as expected Option<Path<T>> extractor does not return None Feb 2, 2025
@jplatte
Copy link
Member

jplatte commented Feb 2, 2025

We should probably improve the documentation for this. Option<Path<T>> is for when you wsnt to have multiple routes that use the same handler function, where one has parameters and the other one not. Since you only have one route in your example, you always get the same behavior. Try adding .route("/", get(handler)).

@demhadais
Copy link
Author

Oh wow I should have definitely figured that out lol. Thanks for your help! I'd be happy to submit a PR with a small addition to the docs if it'd help

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