diff --git a/axum-macros/CHANGELOG.md b/axum-macros/CHANGELOG.md index 102121e7..1a425492 100644 --- a/axum-macros/CHANGELOG.md +++ b/axum-macros/CHANGELOG.md @@ -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* diff --git a/axum-macros/src/from_request/mod.rs b/axum-macros/src/from_request/mod.rs index bb4a8aab..a2432e74 100644 --- a/axum-macros/src/from_request/mod.rs +++ b/axum-macros/src/from_request/mod.rs @@ -725,18 +725,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 // // ``` @@ -805,6 +793,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) @@ -834,7 +835,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) } } } @@ -857,7 +858,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) } } } diff --git a/axum-macros/tests/from_request/pass/container.rs b/axum-macros/tests/from_request/pass/container.rs index 9d4e0666..35fb2a75 100644 --- a/axum-macros/tests/from_request/pass/container.rs +++ b/axum-macros/tests/from_request/pass/container.rs @@ -1,6 +1,7 @@ -use axum::{ - extract::{FromRequest, Json}, - response::Response, +use axum::extract::{ + rejection::JsonRejection, + FromRequest, + Json, }; use serde::Deserialize; @@ -14,7 +15,7 @@ struct Extractor { fn assert_from_request() where - Extractor: FromRequest<(), Rejection = Response>, + Extractor: FromRequest<(), Rejection = JsonRejection>, { } diff --git a/axum-macros/tests/from_request/pass/container_parts.rs b/axum-macros/tests/from_request/pass/container_parts.rs index c90703d0..32164025 100644 --- a/axum-macros/tests/from_request/pass/container_parts.rs +++ b/axum-macros/tests/from_request/pass/container_parts.rs @@ -1,6 +1,7 @@ -use axum::{ - extract::{Extension, FromRequestParts}, - response::Response, +use axum::extract::{ + rejection::ExtensionRejection, + Extension, + FromRequestParts, }; #[derive(Clone, FromRequestParts)] @@ -13,7 +14,7 @@ struct Extractor { fn assert_from_request() where - Extractor: FromRequestParts<(), Rejection = Response>, + Extractor: FromRequestParts<(), Rejection = ExtensionRejection>, { } diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 3ffb9342..f1b61242 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -8,11 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased - **breaking:** Router fallbacks are now properly merged for nested routers ([#3158]) +- **breaking:** `#[from_request(via(Extractor))]` now uses the extractor's + rejection type instead of `axum::response::Response` ([#3261]) - **added:** Implement `OptionalFromRequest` for `Multipart` ([#3220]) - **changed:** `serve` has an additional generic argument and can now work with any response body type, not just `axum::body::Body` ([#3205]) [#3158]: https://github.com/tokio-rs/axum/pull/3158 +[#3261]: https://github.com/tokio-rs/axum/pull/3261 [#3205]: https://github.com/tokio-rs/axum/pull/3205 [#3220]: https://github.com/tokio-rs/axum/pull/3220