Skip to content

[REQ] [rust-axum] support text/event-stream #21967

Open

Description

@jacob-vincent-mink

Is your feature request related to a problem? Please describe.

Currently, the rust-axum server generator only supports application/json responses. This feature request is for the generator to also support endpoints that respond with a stream or MAY respond with a stream.

Describe the solution you'd like

Ideally, the solution is for the traits created for a route to take into account a return value enum that is either a singular value or is a Box<dyn futures::Stream<Item=T> + Send + 'static> (and potentially Unpin?). Then in the route implementation, I can decide if I stream or not and return the appropriate enum variant.

Describe alternatives you've considered

Do it myself by modifying the generated source code in my project - not currently aware of any other options available within this tool.

Additional context

Filing a new ticket for this after quickly discussing with @linxGnu on another ticket. I am interested in contributing on something like this in order to speed up the process, if possible - but I would definitely need guidance and a little hand-holding. This feels like a larger feature than a regular old “first issue”.

Activity

  1. linxGnu commented on Oct 7, 2025

    @linxGnu
    Contributor

    @jacob-vincent-mink

    As recent release for anyOf, oneOf; I think it's time for this feature. Please let me know if you can make PR

  2. jacob-vincent-mink commented on Oct 7, 2025

    @jacob-vincent-mink
    Author

    Hi @linxGnu - like I mentioned, definitely interested. I guess I would need to be pointed pretty explicitly at

    1. Samples of this behavior in another language in the language processor
    2. Samples of this behavior in another mustache template

    I think there’s also some design to discuss - namely, I can describe how I edited the generated code in order to enable streaming myself.

    The key I found was I needed a way to sometimes return a result matching the type signature in https://docs.rs/axum/latest/axum/body/struct.Body.html#method.from_stream. This created a good amount of issues around how to box the item appropriately in an Enum variant for the return type so that the actual route could match on it.

    What I ended up doing was using Axum’s IntoResponse trait as the return type (impl IntoResponse) and pulling some of the generated code down into my api trait implementation. This let me decide if I wanted to stream or not and simply return and object that could be turned into a response (in particular, a body of whichever kind I wanted).

    I don’t believe my implementation is “correct” - it changes the return type and isolation of the trait too much. Maybe I could get your thoughts on the right way to go - simply boxing the stream in an enum variant? Or another direction entirely?

  3. linxGnu commented on Oct 7, 2025

    @linxGnu
    Contributor

    @jacob-vincent-mink
    I think we should directly utilize: https://docs.rs/axum/0.8.6/axum/response/sse/index.html

    Thus, returning Sse<impl Stream<Item = Result<Event, Infallible>>> from request handler. a.k.a, force user to return it

  4. linxGnu commented on Oct 7, 2025

    @linxGnu
    Contributor

    From user perspective:

    impl Product for Controller {
       ....
          async fn streaming_all_products(....) -> Result<Sse<impl Stream<Item = Result<Product, Infallible>>>, Error> {
                  // fetch from DB
                  // process
                  return stream
          }
        ...
    }
  5. jacob-vincent-mink commented on Oct 7, 2025

    @jacob-vincent-mink
    Author

    Good, I like that proposal. One thing I would add is that instead of forcing the user to yield Event as apart of the item, we should implement Into on any type that will be streamed.

    Still leaves a few questions on boxing the SSE<…> but I think we’ll have to figure those out as we go.

  6. linxGnu commented on Oct 8, 2025

    @linxGnu
    Contributor

    @jacob-vincent-mink Yes, anything that could "Into" TryStream

    Please go ahead with your implementation

  7. jacob-vincent-mink commented on Oct 8, 2025

    @jacob-vincent-mink
    Author

    Hi @linxGnu - I spent the day trying to research the best way to go about this and came up empty handed. I don’t know enough about the codebase to implement this feature without some very direct guidance on what to add & change.

  8. linxGnu commented on Oct 8, 2025

    @linxGnu
    Contributor

    Hi @wing328

    I wonder if we have any guideline/book about OpenAPI Generator Engine, specially flow & hooks (pre-processors, post-processors). I think it will help @jacob-vincent-mink a lot to understand the codebase, not only Rust Axum but other generators

  9. jacob-mink-1996 commented on Oct 9, 2025

    @jacob-mink-1996

    Perhaps I was a little dramatic yesterday :)
    I worked with a friend to understand the code a little deeper and figured out I needed to modify the fromOperations method and some other related spots. Please see the PR linked that implements this feature for stream and multi-part content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions