GREsau / okapi

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

Rocket: Using external request-guards makes documenting impossible #113

Closed open-schnick closed 1 year ago

open-schnick commented 1 year ago

In cases where a crate provides a type and a request-guard implementation it is not possible to document endpoints using that type. This was an error for the rocket_db_pools::Connection<T> type and fixed by implementing that trait in this crate in pr #104. I have the same error now with a different type. Obviously we cannot do that for every crate that interacts with rocket. The only way to fix this would be to be able to skip some parameters in the route definition. This was already requested in #85. Sadly i am currently not able to create a pr for that but i would imagine it like so:

#[openapi(ignore = ["param"])]
#[get("/")]
pub fn index(param: ExternalType) -> &'static str {
    // logic
}

My really stupid, but working workaround is to define the same route again, documenting it, and removing the not working parameter.

// the actual route
#[openapi]
#[get("/", rank = 1)]
pub fn index(param: ExternalType) -> &'static str {
    // logic
}

// duplicated route without actual logic but incremented rank
#[openapi]
#[get("/", rank = 2)]
pub fn index_but_documented() -> &'static str {
  // nothing but a warning
}

Obviously that is not really cool. The only way is see to fix this would implement said ignore attributes. What do you think?

ralpha commented 1 year ago

I would have to look further into this. But this looks definitely like something worth looking into more.

I think the ignore approach also looks good. I will see if I can quickly get this solved.

ralpha commented 1 year ago

Solved in master, see https://github.com/GREsau/okapi/blob/master/examples/openapi_attributes/src/main.rs#L14 for an example. I'll document it and other derive attributes later. Right now it works on everything except path parameters because I think this would always give unwanted results.

It will also give compiler errors when the argument does not exist and when it is a path parameter.

Multiple attributes can also be ignored using: #[openapi(ignore = "db", ignore = "log")]

See if this solves your problem. If not or you find a bug, let me know so I can fix it before the next release.

open-schnick commented 1 year ago

Very cool thank you for implementing that <3