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
112 stars 8 forks source link

Actix-web query string parameters not included in spec as parameters #19

Closed ulf-stravito closed 2 months ago

ulf-stravito commented 5 months ago

Hi!

I've been testing out the library to generate a spec for an actix-web service and noticed that my query string parameters are not included in the generated spec. example code:

use actix_web::{web::{Json, Query}, App, HttpServer};
use oasgen::{oasgen, OaSchema, Server};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, OaSchema)]
struct SendCode {
    code: String,
}

#[oasgen]
async fn route(info: Query<SendCode>) -> Json<SendCode> {
    Json(info.into_inner())
}

#[actix_web::main]
async fn main() {

    let server = Server::actix()
        .get("/route", route)
        .write_and_exit_if_env_var_set("openapi.yml")
        .freeze();

    HttpServer::new(move || App::new()
        .service(server.clone().into_service()))
        .bind(("127.0.0.1", 8080))
        .unwrap()
        .run()
        .await
        .unwrap()
}

resulting openapi.yml:

openapi: 3.0.3
info:
  title: ''
  version: ''
paths:
  /route:
    get:
      operationId: route
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SendCode'
components:
  schemas:
    SendCode:
      type: object
      properties:
        code:
          type: string
      required:
      - code

I'd have expected there to be a parameters property for the "route"-path.

I found issue #11 and saw that there was a test for this where this behavior seemed to be expected. And I found that there was a "serde_qs" feature flag, so I tried using the QsQuery-struct from that crate instead:

use actix_web::{web::Json, App, HttpServer};
use oasgen::{oasgen, OaSchema, Server};
use serde::{Deserialize, Serialize};
use serde_qs::actix::QsQuery;

#[derive(Serialize, Deserialize, OaSchema)]
struct SendCode {
    code: String,
}

#[oasgen]
async fn route(info: QsQuery<SendCode>) -> Json<SendCode> {
    Json(info.into_inner())
}

#[actix_web::main]
async fn main() {

    let server = Server::actix()
        .get("/route", route)
        .write_and_exit_if_env_var_set("openapi.yml")
        .freeze();

    HttpServer::new(move || App::new()
        .service(server.clone().into_service()))
        .bind(("127.0.0.1", 8080))
        .unwrap()
        .run()
        .await
        .unwrap()
}

But this will not compile:

15:34:00 [wip !?] [prod-readwrite] $ cargo build
error[E0599]: no function or associated item named `parameters` found for struct `QsQuery<SendCode>` in the current scope
  --> src/main.rs:12:1
   |
12 | #[oasgen]
   | ^^^^^^^^^ function or associated item not found in `QsQuery<SendCode>`
   |
   = note: this error originates in the attribute macro `oasgen` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no function or associated item named `body_schema` found for struct `QsQuery<SendCode>` in the current scope
  --> src/main.rs:12:1
   |
12 | #[oasgen]
   | ^^^^^^^^^ function or associated item not found in `QsQuery<SendCode>`
   |
   = note: this error originates in the attribute macro `oasgen` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0599`.
error: could not compile `router-service` (bin "router-service") due to 2 previous errors

This is the Cargo.toml

[package]
name = "router-service"
version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "4.7.0"
oasgen = { version = "0.20.1", features = ["actix", "serde_qs"] }
serde = { version = "1.0.203", features = ["derive"] }
serde_qs = { version = "0.13.0", features = ["actix4"] }

Am I trying to do something wrong/unsupported? I noticed that this seems to be tested in the case of axum.

Appreciate any pointers in the right direction! :)

kurtbuilds commented 5 months ago

I don't think QueryQs is wired up with actix, only axum.

I'm not using actix anymore, so supporting it has been best effort and/or community driven for now.

I welcome any PRs to fix it, but unfortunately I don't have bandwidth to dive into it myself.

kurtbuilds commented 2 months ago

Believe this is fixed now.