Skip to content

Use via extractor's rejection instead of axum::response::Response #3261

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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions axum-macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# Unreleased

- **breaking:** `#[from_request(via(Extractor))]` now uses the extractor's
rejection type instead of `axum::response::Response` ([#3261])

[#3261]: https://github.com/tokio-rs/axum/pull/3261

# 0.5.0

*No changes since alpha.1*
Expand Down
29 changes: 15 additions & 14 deletions axum-macros/src/from_request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,18 +732,6 @@ fn impl_struct_by_extracting_all_at_once(

let path_span = via_path.span();

let (associated_rejection_type, map_err) = if let Some(rejection) = &rejection {
let rejection = quote! { #rejection };
let map_err = quote! { ::std::convert::From::from };
(rejection, map_err)
} else {
let rejection = quote! {
::axum::response::Response
};
let map_err = quote! { ::axum::response::IntoResponse::into_response };
(rejection, map_err)
};

// for something like
//
// ```
Expand Down Expand Up @@ -811,6 +799,19 @@ fn impl_struct_by_extracting_all_at_once(
quote! { Self }
};

let associated_rejection_type = if let Some(rejection) = &rejection {
quote! { #rejection }
} else {
match tr {
Trait::FromRequest => quote! {
<#via_path<#via_type_generics> as ::axum::extract::FromRequest<#trait_generics>>::Rejection
},
Trait::FromRequestParts => quote! {
<#via_path<#via_type_generics> as ::axum::extract::FromRequestParts<#trait_generics>>::Rejection
},
}
};

let value_to_self = if generic_ident.is_some() {
quote! {
#ident(value)
Expand Down Expand Up @@ -840,7 +841,7 @@ fn impl_struct_by_extracting_all_at_once(
<#via_path<#via_type_generics> as ::axum::extract::FromRequest<_, _>>::from_request(req, state)
.await
.map(|#via_path(value)| #value_to_self)
.map_err(#map_err)
.map_err(::std::convert::From::from)
}
}
}
Expand All @@ -863,7 +864,7 @@ fn impl_struct_by_extracting_all_at_once(
<#via_path<#via_type_generics> as ::axum::extract::FromRequestParts<_>>::from_request_parts(parts, state)
.await
.map(|#via_path(value)| #value_to_self)
.map_err(#map_err)
.map_err(::std::convert::From::from)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions axum-macros/tests/from_request/pass/container.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use axum::{
extract::{FromRequest, Json},
response::Response,
use axum::extract::{
rejection::JsonRejection,
FromRequest,
Json,
};
use serde::Deserialize;

Expand All @@ -14,7 +15,7 @@ struct Extractor {

fn assert_from_request()
where
Extractor: FromRequest<(), Rejection = Response>,
Extractor: FromRequest<(), Rejection = JsonRejection>,
{
}

Expand Down
9 changes: 5 additions & 4 deletions axum-macros/tests/from_request/pass/container_parts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use axum::{
extract::{Extension, FromRequestParts},
response::Response,
use axum::extract::{
rejection::ExtensionRejection,
Extension,
FromRequestParts,
};

#[derive(Clone, FromRequestParts)]
Expand All @@ -13,7 +14,7 @@ struct Extractor {

fn assert_from_request()
where
Extractor: FromRequestParts<(), Rejection = Response>,
Extractor: FromRequestParts<(), Rejection = ExtensionRejection>,
{
}

Expand Down
7 changes: 7 additions & 0 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# Unreleased

- **breaking:** `#[from_request(via(Extractor))]` now uses the extractor's
rejection type instead of `axum::response::Response` ([#3261])

[#3261]: https://github.com/tokio-rs/axum/pull/3261

# 0.8.3

- **added:** Implement `From<Bytes>` for `Message` ([#3273])
Expand Down