utoipa icon indicating copy to clipboard operation
utoipa copied to clipboard

error: struct is not supported in `trait`s or `impl`s. Add support for trait handlers

Open incker opened this issue 1 year ago • 1 comments

Hello

Error is rather clear. utoipa wants my routs be functions only. Let this issue be a feature request

If you have any good solution how to solve this error in my case without making route a function let me please know Code example how I receive this error:

pub struct StatusHandler {
    s: Option<AuthMethod>,
    d: Arc<Dashboard>,
}

impl StatusHandler {
    pub fn new(s: Option<AuthMethod>, d: Arc<Dashboard>) -> Self {
        StatusHandler { s, d }
    }
}

#[async_trait]
impl HttpHandler for StatusHandler {
    type Reply = WithStatus<Json>;

    #[utoipa::path(
        get,
        path = "/pets/{id}",
        responses(
            (status = 200, description = "Pet found succesfully", body = Pet),
            (status = NOT_FOUND, description = "Pet was not found")
        ),
        params(
            ("id" = u64, Path, description = "Pet database id to get Pet for"),
        )
    )]
    async fn handle(self) -> Result<Self::Reply, Infallible> {
        let status = StatusQuery(self.s).query(self.d.as_ref()).await.expect("infallible");
        Ok(with_status(json(&status), StatusCode::OK))
    }
}

incker avatar Jun 06 '23 08:06 incker