kurtbuilds / oasgen

Generates OpenAPI 3.0 spec based on Rust code. Works with axum, actix-web, or any/no framework.
https://crates.io/crates/oasgen
108 stars 9 forks source link

actix: path param doesn't work #22

Closed niklasad1 closed 1 month ago

niklasad1 commented 1 month ago

Hey,

I'm not sure if I'm doing something wrong but I have code such as this:

async fn main() {
           let server = oasgen::Server::actix()
                .route_json_spec("/docs/openapi.json")
                .route_yaml_spec("/docs/openapi.yaml")
                .swagger_ui("/docs/")
                .get("/submissions/{n}", most_recent_submissions)
                .freeze();

            HttpServer::new(move || {
                App::new()
                    .app_data(web::Data::new(db2.clone()))
                    .service(server.clone().into_service())
            })
            .bind(listen_addr)?
            .run()
            .await
}

#[oasgen]
pub async fn most_recent_submissions(
    db: web::Data<Database>,
    n: web::Path<usize>,
) -> Result<Json<Vec<Submission>>, Error> {
    let n = into_non_zero_usize(n.into_inner())?;
    let submissions = db
        .get_most_recent_submissions(n)
        .await
        .map_err(BoxError::from)?;
    Ok(Json(submissions))
}

I'm expecting it to generate path parameters such as https://swagger.io/docs/specification/describing-parameters/#path-parameters but it generates the following yaml:

openapi: 3.0.3
info:
  title: ''
  version: ''
paths:
  /submissions:
    get:
      operationId: routes_all_submissions
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Submission'

Am I doing something wrong or is it not supported?

kurtbuilds commented 1 month ago

Yeah I think this is a bug. Actix hasn't received as much love as the axum side.

If you look at axum::Path, fixing one half of it is really simple. The problem is that it's hard to capture the parameter name. One solution would be to modify the parameter name when it's added with .get. The other solution is probably annotations.

I won't be able to get to this until the weekend, but PR welcome in the meantime.

niklasad1 commented 1 month ago

Ok, no rush I switched to axum instead

kurtbuilds commented 1 month ago

Believe this is fixed now.