Skip to content

Commit 0832de4

Browse files
authored
Use via extractor's rejection instead of axum::response::Response (#3261)
1 parent 6bc0717 commit 0832de4

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

axum-macros/CHANGELOG.md

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

8+
# Unreleased
9+
10+
- **breaking:** `#[from_request(via(Extractor))]` now uses the extractor's
11+
rejection type instead of `axum::response::Response` ([#3261])
12+
13+
[#3261]: https://github.com/tokio-rs/axum/pull/3261
14+
815
# 0.5.0
916

1017
*No changes since alpha.1*

axum-macros/src/from_request/mod.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -725,18 +725,6 @@ fn impl_struct_by_extracting_all_at_once(
725725

726726
let path_span = via_path.span();
727727

728-
let (associated_rejection_type, map_err) = if let Some(rejection) = &rejection {
729-
let rejection = quote! { #rejection };
730-
let map_err = quote! { ::std::convert::From::from };
731-
(rejection, map_err)
732-
} else {
733-
let rejection = quote! {
734-
::axum::response::Response
735-
};
736-
let map_err = quote! { ::axum::response::IntoResponse::into_response };
737-
(rejection, map_err)
738-
};
739-
740728
// for something like
741729
//
742730
// ```
@@ -805,6 +793,19 @@ fn impl_struct_by_extracting_all_at_once(
805793
quote! { Self }
806794
};
807795

796+
let associated_rejection_type = if let Some(rejection) = &rejection {
797+
quote! { #rejection }
798+
} else {
799+
match tr {
800+
Trait::FromRequest => quote! {
801+
<#via_path<#via_type_generics> as ::axum::extract::FromRequest<#trait_generics>>::Rejection
802+
},
803+
Trait::FromRequestParts => quote! {
804+
<#via_path<#via_type_generics> as ::axum::extract::FromRequestParts<#trait_generics>>::Rejection
805+
},
806+
}
807+
};
808+
808809
let value_to_self = if generic_ident.is_some() {
809810
quote! {
810811
#ident(value)
@@ -834,7 +835,7 @@ fn impl_struct_by_extracting_all_at_once(
834835
<#via_path<#via_type_generics> as ::axum::extract::FromRequest<_, _>>::from_request(req, state)
835836
.await
836837
.map(|#via_path(value)| #value_to_self)
837-
.map_err(#map_err)
838+
.map_err(::std::convert::From::from)
838839
}
839840
}
840841
}
@@ -857,7 +858,7 @@ fn impl_struct_by_extracting_all_at_once(
857858
<#via_path<#via_type_generics> as ::axum::extract::FromRequestParts<_>>::from_request_parts(parts, state)
858859
.await
859860
.map(|#via_path(value)| #value_to_self)
860-
.map_err(#map_err)
861+
.map_err(::std::convert::From::from)
861862
}
862863
}
863864
}

axum-macros/tests/from_request/pass/container.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use axum::{
2-
extract::{FromRequest, Json},
3-
response::Response,
1+
use axum::extract::{
2+
rejection::JsonRejection,
3+
FromRequest,
4+
Json,
45
};
56
use serde::Deserialize;
67

@@ -14,7 +15,7 @@ struct Extractor {
1415

1516
fn assert_from_request()
1617
where
17-
Extractor: FromRequest<(), Rejection = Response>,
18+
Extractor: FromRequest<(), Rejection = JsonRejection>,
1819
{
1920
}
2021

axum-macros/tests/from_request/pass/container_parts.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use axum::{
2-
extract::{Extension, FromRequestParts},
3-
response::Response,
1+
use axum::extract::{
2+
rejection::ExtensionRejection,
3+
Extension,
4+
FromRequestParts,
45
};
56

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

1415
fn assert_from_request()
1516
where
16-
Extractor: FromRequestParts<(), Rejection = Response>,
17+
Extractor: FromRequestParts<(), Rejection = ExtensionRejection>,
1718
{
1819
}
1920

axum/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
# Unreleased
99

1010
- **breaking:** Router fallbacks are now properly merged for nested routers ([#3158])
11+
- **breaking:** `#[from_request(via(Extractor))]` now uses the extractor's
12+
rejection type instead of `axum::response::Response` ([#3261])
1113
- **added:** Implement `OptionalFromRequest` for `Multipart` ([#3220])
1214
- **changed:** `serve` has an additional generic argument and can now work with any response body
1315
type, not just `axum::body::Body` ([#3205])
1416

1517
[#3158]: https://github.com/tokio-rs/axum/pull/3158
18+
[#3261]: https://github.com/tokio-rs/axum/pull/3261
1619
[#3205]: https://github.com/tokio-rs/axum/pull/3205
1720
[#3220]: https://github.com/tokio-rs/axum/pull/3220
1821

0 commit comments

Comments
 (0)