Skip to content

Routing doesn't work for two non-overlapping routes #2331

@Thomasdezeeuw

Description

@Thomasdezeeuw

Expected Behavior

I expect the following code two produce two distinct routes:

use std::net::SocketAddr;

use actix_web::{web, App, HttpServer, Responder};

#[actix_web::main]
async fn main() {
    let address: SocketAddr = "127.0.0.1:8080".parse().unwrap();

    let server = HttpServer::new(move || {
        App::new().service(
            web::scope("/api/v0").service(
                web::scope("/collection")
                    // FIXME: this routing doesn't work.
                    // NOTE: switching these two routes doesn't work.
                    .route("/{uuid}", web::get().to(by_uuid))
                    .route("/user/{user_name}/{unqualified_id}", web::get().to(by_id)),
            ),
        )
    })
    .bind(address)
    .unwrap();

    println!("http://{}", address);
    server.run().await.unwrap();
}

pub async fn by_uuid(uuid: web::Path<String>) -> impl Responder {
    format!("by UUID: {}", uuid)
}

pub async fn by_id(
    user_name: web::Path<String>,
    unqualified_id: web::Path<String>,
) -> impl Responder {
    format!("by id: {}/{}", user_name, unqualified_id)
}

Current Behavior

However collection/user/{user_name}/{id} route doesn't work:

$ curl 127.0.0.1:8080/api/v0/collection/abc
by UUID: abc                              # Expected
$ curl 127.0.0.1:8080/api/v0/collection/user/USER/ID
wrong number of parameters: 2 expected 1  # Unexpected

It seem Actix is routing the request to the uuid route and hitting an error.

Steps to Reproduce (for bugs)

  1. Run the code above.
  2. Run curl 127.0.0.1:8080/api/v0/collection/abc, all good.
  3. Run curl 127.0.0.1:8080/api/v0/collection/user/USER/ID, hits an error.

Your Environment

  • Actix Web Version: 4.0.0-beta.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions