GREsau / GREsau/okapi

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

Open
#162 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
740
Forks
127
PR merge metrics
No merged PRs in 30d

Description

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:

```rust
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

```rust
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, 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?")]
pub async fn delay(ms: u64) {}
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.