restrustswaggeropenapirust-axum

Overlapping method route | Rust axum (utoipa)


Here is the problem. I'm using utoipa to generate documentation. And I want to split routes by groups. For example, in axum we can do something like this and it will works fine:

//main.rs
Router::new().route("/user", Router::new().route("/test1", test_handler_1)
                                          .route("/test2", test_handler_2)
                                          .route("/test3", test_handler_3))

So, I can access all of this routes even they have equal method (POST/GET as an example).

But in utoipa for some reason I can't do that. In my project I've splitted all groups of routes to different files like user_handler, product_handler and created there functions that returns OpenApiRouter (user_routes, product_routes).

//product_handler.rs
pub fn product_routes() -> OpenApiRouter<Arc<Pool>> {
    OpenApiRouter::new()
    .routes(routes!(
        ...
        post_product,
        post_product_type,
        ...
    ))
}
//main.rs
OpenApiRouter::with_openapi(ApiDoc::openapi()).nest("/products", product_routes())

But.. I got Overlapping method route. Cannot add two method routes that both handle 'POST' (or GET) note: run with 'RUST_BACKTRACE=1' environment variable to display a backtrace

Any suggestions? Thanks in advance!


Solution

  • For the current moment only one solution available:

    ...
    OpenApiRouter::new()
        .routes(routes!(get_1))
        .routes(routes!(get_2))
        .routes(routes!(get_3))
    ...
    

    The main thing: axum's route created specifically for unique methods (only one post and get may be specified per route). So, enjoy the solution! =)