GREsau / okapi

OpenAPI (AKA Swagger) document generation for Rust projects
MIT License
578 stars 103 forks source link

openapi doesn't ignore Rocket request guards? #119

Closed ChronosWS closed 1 year ago

ChronosWS commented 1 year ago

I have an API implemented thus:

#[openapi]
#[get("/job/namespaces/<namespace>/users/<user_id>")]
async fn get_user_jobs(
    namespace: &str,
    user_id: UserId,
    job_state: &State<JobSharedState>,
    _user_authorization: UserAuthorization
) -> Json<Option<JobData>> {
    let mut job_state = job_state.write().await;
    Json(logic::get_user_jobs(namespace, user_id, &mut job_state)
        .await)
}

This generates an error:

the trait bound `UserAuthorization: OpenApiFromRequest<'_>` is not satisfied
the following other types implement trait `OpenApiFromRequest<'a>`:
  &'r ContentType
  &'r Host<'r>
  &'r Limits
  &'r Route
  &'r rocket::Config
  &'r rocket::State<T>
  &'r rocket::http::Accept
  &'r rocket::http::CookieJar<'r>
and 8 others

I feel like this UserAuthorization - which exists to implement an authentication guard - should not be part of the API spec. But it also seems like this might be a common pattern so maybe I am unaware of how to make okapi do the right thing?

ralpha commented 1 year ago

If you just want to ignore it: See #113 (recent addition, not released yet, only in master)

But you might also want to take a look at this example: https://github.com/GREsau/okapi/tree/master/examples/secure_request_guard/src This example shows how you can implement different authentication methods into the spec. This way you can still use the try buttons in the swagger/rapidoc. So definitely worth a look in my opinion.

ChronosWS commented 1 year ago

Thank you! I knew I must have missed something!