actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Multiple `.service(` doesn't merge route patterns

Open SamuelMarks opened this issue 3 months ago • 0 comments

Expected Behavior

I'm building a nice size Apache-2.0 OR MIT) application. Reads are insecure, writes are secured. Here's my server:

.service(
    utoipa_actix_web::scope("/api/v0")
        .wrap(actix_web::middleware::Compat::new(
            actix_web_httpauth::middleware::HttpAuthentication::bearer(
                rust_actix_diesel_auth_scaffold::middleware::bearer::validator,
            ),
        ))
        /* Org */
        .service(crawlcomply_backend::routes::org::upsert)
        .service(crawlcomply_backend::routes::org::remove)
)
.service(
    utoipa_actix_web::scope("/api/v0")
        /* Org */
        .service(crawlcomply_backend::routes::org::read_many)
)

Current Behavior

POST /api/v0/org works, whereas GET /api/v0/org never hits the route function.

Possible Solution

Merge routes

Steps to Reproduce (for bugs)

actix_web::HttpServer::new(move || {
        actix_web::App::new()
            .into_utoipa_app()
            .openapi(ApiDoc::openapi())
            .map(|app| app.wrap(actix_web::middleware::Logger::default()))
            .app_data(
                actix_web::web::JsonConfig::default().error_handler(|err, _req| {
                    actix_web::error::InternalError::from_response(
                        "",
                        actix_web::HttpResponse::BadRequest()
                            .content_type("application/json")
                            .body(format!(r#"{{"error":"{}"}}"#, err)),
                    )
                    .into()
                }),
            )
            .app_data(actix_web::web::Data::new(pool.clone()))
            .service(
                utoipa_actix_web::scope("/api/v0")
                    .wrap(actix_web::middleware::Compat::new(
                        actix_web_httpauth::middleware::HttpAuthentication::bearer(
                            rust_actix_diesel_auth_scaffold::middleware::bearer::validator,
                        ),
                    ))
                    /* Org */
                    .service(crawlcomply_backend::routes::org::upsert)
                    .service(crawlcomply_backend::routes::org::remove)
            )
            .service(
                utoipa_actix_web::scope("/api/v0")
                    /* Org */
                    .service(crawlcomply_backend::routes::org::read)
                    .service(crawlcomply_backend::routes::org::read_many)
            )
            .openapi_service(|api| utoipa_scalar::Scalar::with_url("/scalar", api))
            .into_app()
    })
    .bind((args.hostname.as_str(), args.port))?
    .run()
    .await

Context

Trying to have one group of routes be authenticated only and another group of routes public.

Your Environment

  • Rust Version (I.e, output of rustc -V): rustc 1.92.0-nightly (caccb4d03 2025-09-24)
  • Actix Web Version: 4.11.0

SamuelMarks avatar Sep 25 '25 16:09 SamuelMarks