Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update axum examples with state #1234

Open
happysalada opened this issue Dec 12, 2024 · 3 comments
Open

Update axum examples with state #1234

happysalada opened this issue Dec 12, 2024 · 3 comments

Comments

@happysalada
Copy link

Could you update the axum code examples of the nested routers to include state ?
As soon as i have a handler that requires state that us nested, thw compiler infers that my router types are stateful and i can never get it to a Router<()> type later to serve it with axum.

@Kimishu
Copy link

Kimishu commented Dec 19, 2024

Could you update the axum code examples of the nested routers to include state ? As soon as i have a handler that requires state that us nested, thw compiler infers that my router types are stateful and i can never get it to a Router<()> type later to serve it with axum.

If I understood ur question correctly, I think u should do something like this. The main thing is to add a generic parameter to Router.

@litcc
Copy link

litcc commented Dec 19, 2024

I'm not sure if the utoipa_axum::routes! macro can handle the following scenarios:
Handler functions that substitute generic parameters

image

Includes middleware added to handler functions when writing routes

image

Previous attempts to use utoipa_axum::routes! , have invariably failed

By the way I would like to make a point that is not related to this issue, please don't be offended:

😄 If possible, please add a little bit more examples of generalizations;
I've noticed that the current support for generalization doesn't seem to be particularly well developed (probably because I don't know enough about the project)

I have a basic response body based on generics, and currently only the ToSchema macro can be used directly, the rest of the code generated by utoipa::IntoResponses, utoipa::ToResponse have generic constraints and can't be used directly.

image

@RobbieMcKinstry
Copy link

I'm currently encountering the same problem as @litcc : substituting generic parameters in handlers signatures.

For example, in this code, my create_workspace_handler handler takes a generic. In instantiate it with a concrete PostgresRepository instance in production, but swap that for a MockRepository when testing. This lets me practice the hexagonal architecture as described in this guide.

pub struct WorkspacesController;

impl WorkspacesController {
    pub fn router() -> OpenApiRouter<Deps> {
        OpenApiRouter::new()
            .route(
                "/",
                post(create_workspace_handler::<PostgresRepository>),
            )
    }
}

// Elsehwhere...
pub async fn create_workspace_handler<W: WorkspacesRepository>(
    Extension(workspaces): Extension<W>,
) -> Result<Success, Failure> {
  // Details omitted.
}

Should we open a separate issue to track this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants