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

This commit is contained in:
Kyle Nweeia
2025-07-16 09:50:59 +00:00
committed by GitHub
parent 6bc0717b06
commit 0832de443c
5 changed files with 35 additions and 22 deletions
+7
View File
@@ -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*
+15 -14
View File
@@ -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)
}
}
}
@@ -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>,
{
}
@@ -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>,
{
}
+3
View File
@@ -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