tower-rs / tower-http

HTTP specific Tower utilities.
675 stars 156 forks source link

`axum::Router.layer` complaining about `tower_http::cors::CorsLayer`, fixed with tower downgrade #444

Closed databasedav closed 9 months ago

databasedav commented 9 months ago

Bug Report

Version

0.5.0

Platform

Ubuntu 23.04

Repro

[package]
name = "tower_test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.6.20"
serde = { version = "1.0.193", features = ["derive"] }
tower-http = { version = "0.5.0", features = ["cors"] }
use axum::{self, response::IntoResponse};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Payload;

async fn endpoint(axum::Json(payload): axum::Json<Payload>) -> impl axum::response::IntoResponse {
    axum::http::StatusCode::OK.into_response()
}

fn make_app() -> axum::Router {
    axum::Router::new()
    .route("/endpoint", axum::routing::post(endpoint))
    .layer(tower_http::cors::CorsLayer::permissive())
}

fn main() {
    make_app();
}
error[E0277]: the trait bound `Cors<Route<_>>: tower_service::Service<axum::http::Request<_>>` is not satisfied
   --> src/main.rs:14:12
    |
14  |     .layer(tower_http::cors::CorsLayer::permissive())
    |      ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `tower_service::Service<axum::http::Request<_>>` is not implemented for `Cors<Route<_>>`
    |      |
    |      required by a bound introduced by this call
    |
    = help: the trait `tower_service::Service<http::request::Request<ReqBody>>` is implemented for `Cors<S>`
note: required by a bound in `Router::<S, B>::layer`
   --> /home/avi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.6.20/src/routing/mod.rs:236:21
    |
233 |     pub fn layer<L, NewReqBody>(self, layer: L) -> Router<S, NewReqBody>
    |            ----- required by a bound in this associated function
...
236 |         L::Service: Service<Request<NewReqBody>> + Clone + Send + 'static,
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Router::<S, B>::layer`

running cargo add tower-http@0.4.4 fixes the issue, i also tried downgrading axum but the issue persisted

databasedav commented 9 months ago

o this looks like an http-body compatibility issue that will be fixed with the next axum release? just some bad timing on my part :sweat_smile:

jplatte commented 9 months ago

Yes, we have at least two existing issues about this. These versions of axum and tower-http are not compatible since one uses http 1.0 and the other uses http 0.2. This is far from the first issue about this. If you want to use tower-http 0.5 with axum, it has to be at least axum 0.7.