Skip to content

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

@davidpdrsn

Description

@davidpdrsn

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 😞

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions