Skip to content

How to use axum::extract::State with #[oasgen] in handlers? #32

@tgrushka

Description

@tgrushka

I am using handlers that automatically extract state with less boilerplate like this:

pub type Tx = axum_sqlx_tx::Tx<sqlx::Postgres>;

pub type TxState = axum_sqlx_tx::State<sqlx::Postgres>;

#[derive(Clone)]
pub struct ApiState {
    pub config: Config,
    pub tx: TxState,
    pub redis: Redis,
}

pub fn jobs_routes() -> Router<ApiState> {
    Router::new()
...
        .route("/checkout", patch(checkout_job))
...
}

#[oasgen]
pub async fn checkout_job(mut tx: Tx, config: Config) -> ApiResult<CheckoutJobResponse> {
...
}

But I can't use oasgen with this, because it thinks tx and config should be params, when they are internal axum state extractors and should definitely not be API params.

Errors:

the trait bound `axum_sqlx_tx::Tx<Postgres>: OaParameter` is not satisfied...
the trait bound `config::Config: OaParameter` is not satisfied...

Is there a workaround? I think a lot of people use axum this way. I even tried wrapping in State<T> like this:

#[oasgen]
pub async fn checkout_job(
    State(mut tx): State<Tx>,
    State(config): State<Config>,
) -> ApiResult<CheckoutJobResponse> {
...
}

Which is still too much boilerplate for me anyway, but still get errors:

the trait bound `axum::extract::State<axum_sqlx_tx::Tx<Postgres>>: OaParameter` is not satisfied
...
the trait bound `axum::extract::State<config::Config>: OaParameter` is not satisfied

Obviously OaParameter can't be implemented for external crates, and I wouldn't want to, anyway, as these are internal and should never be exposed to the API.

I was looking at this crate as a possible alternative to utoipa because it has a lot of boilerplate and this seems more automatic. Is there any way oasgen could allow configuration or detection of my ApiState or the state of the handlers or something and ignore those parameters entirely?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions