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

How to route Body response used for streaming #152

Open
hniksic opened this issue Nov 4, 2024 · 1 comment
Open

How to route Body response used for streaming #152

hniksic opened this issue Nov 4, 2024 · 1 comment

Comments

@hniksic
Copy link

hniksic commented Nov 4, 2024

I have an endpoint declared like this:

pub async fn handler(...args...) -> Result<Body, Response> {
    // ...
    Ok(Body::from_stream(tokio_util::io::ReaderStream::new(stream)))
}

This handler is not accepted by aide::axum::routing::get_with because it returns Body. (If I change Body to e.g. Vec<u8>, the call to get_with() compiles.) I need to return Body to stream the response to the client. Is there a way to pass such a handler to aide?

@hniksic
Copy link
Author

hniksic commented Feb 21, 2025

As a workaround, I resolved this by using a newtype:

#[derive(OperationIo)]
pub struct ExportBody(Body);

impl IntoResponse for ExportBody {
    fn into_response(self) -> Response {
        self.0.into_response()
    }
}

pub async fn get_audience_export(...args...) -> Result<ExportBody, Response> {
    // ...
    let body = Body::from_stream(tokio_util::io::ReaderStream::new(stream));
    Ok(ExportBody(body))
}

This gets the handler to be accepted by aide::axum::routing::get_with, and the actual return type is documented as Bytes in the separate doc handler.

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

1 participant