Skip to content

axum-extra: implement FromRequest for Either* #3323

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

mladedav
Copy link
Collaborator

Motivation

Either can currently be used only for FromRequestParts extractor. We might want to support extraction of different kinds of bodies. Example of a user wanting something like this is #3315

Solution

Have Either buffer the whole body into Bytes and then try the extractors in turn.

@jplatte
Copy link
Member

jplatte commented Apr 26, 2025

This seems like a little bit of a footgun potentially. Using Either<Json<T>, Form<T>> is going to buffer the request body regardless of the content type.

I'm also wondering whether / how to support Either<impl FromRequest, impl FromRequestParts> or Either<impl FromRequestParts, impl FromRequest>. I'm pretty sure those don't work with this PR as-is.

Any comments / thoughts?

@mladedav
Copy link
Collaborator Author

mladedav commented May 5, 2025

Yeah, it buffers the whole requests. Another option is to instead drive all the extractors at the same time. But I'd expect something like Either3<Json<T>, String, Bytes> to then actually have three copies of all the data in memory so I'd say one buffered reused copy would be preferable.

We might also consider not using Either for this at and make another more descriptive type specifically for this, something like BufferedEither/FromRequestBufferredEither?

As for supporting Either<impl FromRequest, impl FromRequestParts>, I've put together something working, but I originally thought we might not want that. But we can use it.

Then again we probably do want to have a speicifc type for this, because if we implement both FromRequestParts and FromReqeust for the same type, it has two different FromRequest implementations, one with ViaParts and the explicit one. So the way it's set up now, this would break people who have handlers where the last argument is Either* containing only impl FromRequestParts types.

So I guess another type for this would be the way to go? Or just not do this at all and let people clone/try extract requests explicitly?

@yanns
Copy link
Collaborator

yanns commented May 6, 2025

I'm not sure about this.
For people who are aware of the downsides of buffering the body, this is a nice addition. 💯
I'm just afraid of people adopting this without conscientiously acknowledging the trade-offs. 🤷

A possible other approach is to make it easy for handlers to accept a buffered body, and make it easy to run extractors on it.

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

Successfully merging this pull request may close these issues.

3 participants