Search code examples
rustrust-axumaxum-login

How can I use axum-login with axum-typed-routing?


I implemented axum-login following one of the example in their repo, but I ran into this:

#[api_route(GET "/item/:id?amount&offset&test" with AppState {
    summary: "Get an item",
    description: "Get an item by id",
    id: "get-item",
    tags: ["items"],
    hidden: false
    security: { "auth_session": ["read:items"] },
    responses: { 200: String },
})]
async fn item_handler(
    id: u32,
    amount: Option<u32>,
    offset: Option<u32>,
    test: Option<u32>,
    State(state): State<AppState>,
    Json(json): Json<u32>,
    auth_session: AuthSession,
) -> String {
    ...
}
error[E0277]: the trait bound `fn(axum::extract::Path<(u32,)>, axum::extract::Query<__QueryParams__>, axum::extract::State<AppState>, extractors::Json<u32>, axum_login::AuthSession<users::Backend>) -> impl std::future::Future<Output = std::string::String> {__inner__function__}: OperationHandler<_, _>` is not satisfied
   --> src/main.rs:120:1
    |
120 | / #[api_route(GET "/item/:id?amount&offset&test" with AppState {
121 | |     summary: "Get an item",
122 | |     description: "Get an item by id",
123 | |     id: "get-item",
...   |
127 | |     responses: { 200: String },
128 | | })]
    | |___^ the trait `OperationHandler<_, _>` is not implemented for fn item `fn(axum::extract::Path<(u32,)>, axum::extract::Query<__QueryParams__>, axum::extract::State<AppState>, extractors::Json<u32>, axum_login::AuthSession<users::Backend>) -> impl std::future::Future<Output = std::string::String> {__inner__function__}`
    |
note: required by a bound in `get_with`
   --> /Users/gradlon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/aide-0.13.2/src/axum/routing.rs:345:1
    |
345 | method_router_top_level!(get, get_with);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------^
    | |                             |
    | |                             required by a bound in this function
    | required by this bound in `get_with`
    = note: this error originates in the attribute macro `api_route` which comes from the expansion of the macro `method_router_top_level` (in Nightly builds, run with -Z macro-backtrace for more info)

Does anyone know what the problem is? Or what the fix is?


Solution

  • The error is caused by OperationHandler not being implemented for AuthSession. You can solve this by using NoApi to wrap AuthSession in order to skip documenting it.

    Your code should look something like this:

    use aide::NoApi;
    
    type AuthSessionNoApi = NoApi(AuthSession);
    
    #[api_route(GET "/item/:id?amount&offset&test" with AppState {
        summary: "Get an item",
        description: "Get an item by id",
        id: "get-item",
        tags: ["items"],
        hidden: false
        security: { "auth_session": ["read:items"] },
        responses: { 200: String },
    })]
    async fn item_handler(
        id: u32,
        amount: Option<u32>,
        offset: Option<u32>,
        test: Option<u32>,
        state): State<AppState>,
        Json(json): Json<u32>,
        auth_session: AuthSessionNoApi,
    ) -> String {
        ...
    }
    

    For more background information check out this issue.