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

Should Request<B> and Response<B> implement Body? #48

Open
davidpdrsn opened this issue Aug 24, 2021 · 3 comments
Open

Should Request<B> and Response<B> implement Body? #48

davidpdrsn opened this issue Aug 24, 2021 · 3 comments

Comments

@davidpdrsn
Copy link
Member

Today I discovered an unfortunate interaction between axum and http-body.

In axum you're able to write this:

use axum::{
    body::{box_body, Body, BoxBody},
    handler::get,
    http::Response,
    Router,
};

let app = Router::new().route(
    "/",
    get(|| async {
        // Build a response with a header.
        let response: Response<Body> = Response::builder()
            .header("x-foo", "foo")
            .body(Body::empty())
            .unwrap();

        // Since `Response<B>` implements `Body`, we can use `box_body` to
        // convert it into a `BoxBody`.
        let body: BoxBody = box_body(response);

        // And because `BoxBody` implements `IntoResponse` we can return it
        // from handlers.
        //
        // However `impl IntoResponse for BoxBody` simply does
        // `Response::new(self)` thus removing headers, status, etc.
        body
    }),
);

So because Response<B> implements Body it can be used with box_body but that ends up removing everything from the response except the body. I think being able to return bodies directly from axum handlers is a nice feature its just unfortunate that this particular thing compiles.

BoxBody's IntoResponse impl is here.

One could say "well just don't do this" but would be nice if it didn't compile at all, which could be done by removing impl Body for {Response, Request}<B>. Thats of course a breaking change 😞

@seanmonstar
Copy link
Member

It was added for convenience. Some libraries in other languages allow you to treat a request or response directly as a stream, and it seemed useful.

That said, being explicit isn't necessarily a bad thing, especially if it makes something clearer or prevents frequent accidents. I haven't heard of a similar accident since we added that impl until now, so I don't know if it's frequent. But I could be convinced.

We couldn't make that breaking change to hyper, though. Maybe once, for 1.0. Is it worth it?

@davidpdrsn
Copy link
Member Author

One could also argue that axum is too implicit in that a body magically gets turned into a response.

We couldn't make that breaking change to hyper, though. Maybe once, for 1.0. Is it worth it?

Yeah I'm fine with waiting until hyper going 1.0. I don't think its a big issue but it was something that tripped up someone who was new to rust.

@tesaguri
Copy link
Contributor

A user could call a generic function which takes an impl Body with both B and Response<B>and I guess the monomorphization can lead to code bloat.

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

3 participants