tokio-rs/axum

Is "Rewriting request URI in middleware" out of date?

Closed this issue · 8 comments

I asked about this in Discord as well, but I'm not sure anyone saw it.

I'm trying to use NormalizePathLayer from tower-http with axum 0.7 as per this documentation section.

For example, I've tried something like this:

let app: NormalizePath<Router> = NormalizePathLayer::trim_trailing_slash()
     .layer(Router::new().route("/", axum::routing::get(|| async {})));
let listener = tokio::net::TcpListener::bind(addr).await?;
axum::serve(listener, app.into_make_service()).await?;

However, the compiler is unhappy about this and complains:

error[E0284]: type annotations needed
  --> src/web/app.rs:78:35
   |
78 |         axum::serve(listener, app.into_make_service()).await?;
   |                                   ^^^^^^^^^^^^^^^^^
   |
   = note: cannot satisfy < as HttpBody>::Data == axum::body::Bytes
   = note: required for Router to implement Service<http::Request<>>
   = note: 1 redundant requirement hidden
   = note: required for NormalizePath<Router> to implement Service<http::Request<_>>
note: required by a bound in axum::ServiceExt::into_make_service``

Is the documentation current? If so, is this an expected result?

It looks like using a fully-qualified syntax is the approach to take, see #2377.

I'd agree that the docs should probably be updated to reflect this.

Thank you for pointing to that answer, it didn't come up when I searched earlier. The fully-qualified syntax does indeed work.

Yep fully qualified syntax is the way. Its necessary because of how Router implements Service.

@davidpdrsn should the docs be updated then?

Feels like this issue should remain open until then if so.

All examples are checked on CI so it should work.

@davidpdrsn I'm confused, the linked note does not mention using the fully qualified syntax, hence this issue being opened in the first place.

That said, the fully qualified syntax does seem to be required (at least to e.g. use NormalizePathLayer).

This seems like an issue that should be addressed in the documentation, no?

The docs don’t mention NormalizePathLayer so probably something with how that works that makes rust not able to infer the type. You’re welcome to submit a PR that adds that.