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

Implement the OptionalFromRequestParts trait for the Host extractor #3177

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

Conversation

dnlsndr
Copy link

@dnlsndr dnlsndr commented Jan 14, 2025

Fixes: #3170

Motivation

To allow for Option<Host> extractors we need to implement the OptionalFromRequestParts trait for it.

Solution

Implement OptionalFromRequestParts for the Host Extractor

@dnlsndr dnlsndr marked this pull request as ready for review January 14, 2025 19:44
axum-extra/src/extract/host.rs Outdated Show resolved Hide resolved
axum-extra/src/extract/host.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@Turbo87 Turbo87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks :)

@Turbo87 Turbo87 requested a review from jplatte January 15, 2025 08:54
@Turbo87 Turbo87 added C-enhancement Category: A PR with an enhancement A-axum-extra labels Jan 15, 2025
@dnlsndr
Copy link
Author

dnlsndr commented Jan 27, 2025

@jplatte feel free to review :)

@mladedav
Copy link
Collaborator

I don' t hink we want Option to be the same as the normal extractor and just discard errors.

I think it would be better to surface the parsing errors and return None only if there weren't any. I believe that was the idea behind OptionalFromRequestParts, when you just want to discard errors, you should use Result<T, _>. Arguably the full extractor should also differentiate between missing and malformed headers so this might have to wait until the next breaking release.

Others' opinions might differ and we can go ahead with this in that case, if not I'm not sure where that would leave this PR. We could merge it and change it along with the other extractor or just wait with it.

@jplatte
Copy link
Member

jplatte commented Jan 27, 2025

My availability is limited for the next two weeks or so, but I'll get to reviewing it some time next month.

@Turbo87
Copy link
Collaborator

Turbo87 commented Jan 27, 2025

I don' think we want Option to be the same as the normal extractor and just discard errors.

@mladedav the Host extractor in its current state already swallows any parsing errors and just discards the header. the only rejection that it currently returns is FailedToResolveHost, which is equivalent to the None case, so this refactored implementation does not discard more errors than before due to this particular OptionalFromRequestParts implementation using type Rejection = Infallible.

see also #3177 (comment) 😉

@mladedav
Copy link
Collaborator

Yeah, I get that it doesn't discard more errors than the other extractor, but that doesn't change that it does discard them. I appreciate we want the two to work as similarly as possible, but I'm not sure that this addition is really needed then.

The only reason I can see why people want this is because people are used to Option being just Result where all errors are conflated into None. Which I think we should not promote because it already is not the case for some extractors and it will only get more surprising. In the linked issue someone seemed genuinely surprised they can just extract Result to get basically the same thing so I think this is just an issue with education rather than users really wanting Options specifically.

I generally don't think we should have any OptionalFromRequestParts extractors where the implementation can be done with Result::ok. That doesn't mean we should have the optional extractor here, I just think it should mean something slightly different which is arguably also different from what the main extractor does right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum-extra C-enhancement Category: A PR with an enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing OptionalFromRequestParts implementation for the Host extractor from the axum-extra crate
5 participants