okapi icon indicating copy to clipboard operation
okapi copied to clipboard

Shouldn't okapi macros use `path` instead of `expr`?

Open pravic opened this issue 8 months ago • 1 comments

If I want to specify the full path to endpoints in openapi_get_routes_spec (or any other macro), I get the expected identifier error from Rust-analyzer. The Rust compiler itself doesn't complain, though.

Basically, the following should both work but it doesn't:

    use inner::{delay, okapi_add_operation_for_delay_};
    let _ = rocket_okapi::openapi_get_routes_spec![delay];
    let _ = rocket_okapi::openapi_get_routes_spec![inner::delay]; // expected identifier [rust analyzer(macro-error)]
Some code to illustrate the problem
macro_rules! collect_http_endpoints {
    // this macro uses `path`
    (path: $($route:path),* $(,)*) => {{
        let spec = rocket_okapi::openapi_spec![$($route),*];
        let routes = rocket_okapi::openapi_routes![$($route),*];
        (routes, spec)
    }};

    // this macro uses `expr`
    (expr: $($route:expr),* $(,)*) => {{
        let spec = rocket_okapi::openapi_spec![$($route),*];
        let routes = rocket_okapi::openapi_routes![$($route),*];
        (routes, spec)
    }};
}

pub fn collect_endpoints() -> (Vec<rocket::Route>, OpenApi) {
    use inner::{delay, okapi_add_operation_for_delay_};

    // with `path` both imports work well
    let _ = collect_http_endpoints![path: inner::check, delay];

    // with `expr` it works if you import both `endpoint` and `okapi_add_operation_for_endpoint_`
    let _ = collect_http_endpoints![expr: delay];

    // but fails if you want to specify the full path:
    let _ = collect_http_endpoints![expr: inner::check];

    // `rocket_okapi::openapi_get_routes_spec` fails with full path:
    let _ = rocket_okapi::openapi_get_routes_spec![delay];
    let _ = rocket_okapi::openapi_get_routes_spec![inner::check];

    todo!()
}

mod inner {
    use rocket_okapi::openapi;

    #[openapi]
    #[get("/check")]
    pub async fn check() {}

    /// Delay the response by the `ms` milliseconds (up to 10 seconds).
    #[openapi]
    #[get("/delay?<ms>")]
    pub async fn delay(ms: u64) {}
}

pravic avatar May 07 '25 09:05 pravic

Of course, this is more of Rust-analyzer problem but is there a reason for using the expr here? What possible expression would someone want to pass to the macro which evaluates to an endpoint handler path? I think, path is more appropriate here since it handles both imported and full path items.

pravic avatar May 07 '25 09:05 pravic