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

Axum middleware mismatch on types #771

Closed
wduminy opened this issue Jan 9, 2024 · 6 comments
Closed

Axum middleware mismatch on types #771

wduminy opened this issue Jan 9, 2024 · 6 comments

Comments

@wduminy
Copy link
Contributor

wduminy commented Jan 9, 2024

I am upgrading from lambda_http 0.8 to 0.9.0
As a consequence, I also upgraded to axum 0.7.3`

I have a compile error on axum middleware:

Here is a minimal example:

pub async fn mw_sample_1(mut req: lambda_http::Request, next: Next) -> impl IntoResponse {
    next.run(req).await
}

It gives me this error:

error: mismatched types
label: arguments to this method are incorrect
note: `lambda_http::Body` and `axum::body::Body` have similar names, but are actually distinct types
label: arguments to this method are incorrect
note: `lambda_http::Body` is defined in crate `aws_lambda_events`
label: arguments to this method are incorrect
note: `axum::body::Body` is defined in crate `axum_core`
label: arguments to this method are incorrect
note: method defined here
label: arguments to this method are incorrect

If I change the code to this, the error goes away (as expected)

pub async fn mw_sample_2(mut req: axum::extract::Request, next: Next) -> impl IntoResponse {
    next.run(req).await
}

In my middleware, I need the lambda_http::Request.

Before the upgrade, the code looked like this:

pub async fn mw_sample_old(
    mut req: lambda_http::Request,
    next: Next<lambda_http::Body>,
) -> impl IntoResponse {
    next.run(req).await
}

Am I missing something?

@wduminy wduminy changed the title Axum middleware mistmatch on types Axum middleware mismatch on types Jan 9, 2024
@wduminy
Copy link
Contributor Author

wduminy commented Jan 10, 2024

The solution that worked for me was to use this

pub async fn mw_sample_2(req: axum::extract::Request, next: Next) -> impl IntoResponse {
    ...
}

To get to the lambda_http context, do this

    let context = req
        .extensions()
        .get::<lambda_http::request::RequestContext>();

@wduminy
Copy link
Contributor Author

wduminy commented Jan 10, 2024

I think this can be closed

@wduminy wduminy closed this as completed Jan 10, 2024
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for the maintainers of this repository to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@calavera
Copy link
Contributor

@wduminy it'd be great if you could add an example on how to use Axum middlewares in our examples directory. I'm not super familiar with them myself, and we'll appreciate the contribution.

@wduminy
Copy link
Contributor Author

wduminy commented Jan 11, 2024

@calavera Good idea - I'd be happy to add an example.

@wduminy
Copy link
Contributor Author

wduminy commented Jan 20, 2024

PR submitted: #785

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

2 participants