Skip to content

api_operation wraps async fn body in a synchronous closure and renders .awaiting in it invalid #130

Closed
@cafce25

Description

The following code:

use actix_web::Responder;
#[apistos::api_operation(
    tag = "image"
)]
async fn foo() -> impl Responder {
    bar().await;
    todo!()
}
async fn bar() {}

results in:

error[E0728]: `await` is only allowed inside `async` functions and blocks
 --> src/main.rs:6:11
  |
2 | / #[apistos::api_operation(
3 | |     tag = "image"
4 | | )]
  | |__- this is not `async`
5 |   async fn foo() -> impl Responder {
6 |       bar().await;
  |             ^^^^^ only allowed inside `async` functions and blocks

when the user apparently did use an asynchronous function.

There is 2 options to resolve this:

  1. Document the fact that the body of a function annotated with api_operation must be synchronous.
  2. Fix the macro definition

Currently the macro expands to this:

fn foo() -> impl std::future::Future<
    Output = apistos::actix::ResponderWrapper<impl Responder>,
> + apistos::PathItemDefinition {
    let inner = core::future::ready(
        apistos::actix::ResponderWrapper(
            (move || {
                bar().await;
                ::core::panicking::panic("not yet implemented")
            })(),
        ),
    );
    apistos::actix::ResponseWrapper {
        inner,
        path_item: __openapi_foo,
    }
}

it could probably expand to

fn foo() -> impl std::future::Future<
    Output = apistos::actix::ResponderWrapper<impl Responder>,
> + apistos::PathItemDefinition {
    let inner = core::future::ready(
        apistos::actix::ResponderWrapper(
            async move {
                bar().await;
                ::core::panicking::panic("not yet implemented")
            }.await,
        ),
    );
    apistos::actix::ResponseWrapper {
        inner,
        path_item: __openapi_foo,
    }
}

or something similar and allow the use of .await within the body of foo.

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions