GREsau / okapi

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

How to skip attributes in routes #85

Closed HerrMuellerluedenscheid closed 2 years ago

HerrMuellerluedenscheid commented 2 years ago

I was trying to use okapi in a project that uses diesel for database management. Basically as in this example: https://github.com/TmLev/crud-rest-api-rust-rocket-diesel-postgres/blob/65783a859f8b8afe42861cf1a210401a9c24cee9/src/main.rs

Taking for example this signature:

#[rocket::get("/<id>")]
async fn retrieve(
    connection: PgConnection,
    id: i32,
) -> Result<Json<Artist>, NotFound<Json<ApiError>>> {}

okapi would have to skip the first argument connection: PgConnection. I didn't find how to do that if it's possible. Is it possible?

thanks in advance!

HerrMuellerluedenscheid commented 2 years ago

Or should I rather implement OpenApiFromRequest<'_> for db::PgConnection to somehow return nothing?

pintariching commented 2 years ago

I also have the same problem and I'm not sure how to solve it. One solution that could possibly get around this is to have all the routes that do the actual logic under the route /api/stuff/... and maybe have a seperate base route that goes like /openapi/stuff... and have some sort of dummy functions that return the same structs.

For example for your case you could try having a seperate function like this:

#[openapi]
#[rocket::get("/openapi/<id>")
async fn retrieve(id: i32) -> Result<Json<Artist>, NotFound<Json<ApiError>>> { 
  return Json(Artist { ... })
}

Yes this is a very roundabout way of doing it and I'm also looking if there's anything else possible

pintariching commented 2 years ago

Okay now I just feel stupid. The solution is in the README if you scroll a bit lower in the FAQ and click on "Implement OpenApiFromRequest for Diesel DB"

HerrMuellerluedenscheid commented 2 years ago

Lol! I didn't see that either. I didn't really go through the README.md but went straight to the documentation and searched for diesel. If possible it would be worth considering to move the FAQs to some place where they can be found by keyword search.

ralpha commented 2 years ago

Haha, yeah had to put it somewhere so added it there. I'll add it to Rust Docs too so it shows up in searches there too.