Use extensions directly in from_request_parts (#3542)

This commit is contained in:
Brad Dunbar
2025-11-14 20:38:45 +01:00
committed by Jonas Platte
parent fe607e6dcf
commit 6d5e6d578a
+7 -8
View File
@@ -1,4 +1,4 @@
use axum::extract::{Extension, FromRequestParts}; use axum::extract::FromRequestParts;
use http::request::Parts; use http::request::Parts;
/// Cache results of other extractors. /// Cache results of other extractors.
@@ -88,13 +88,12 @@ where
type Rejection = T::Rejection; type Rejection = T::Rejection;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
match Extension::<CachedEntry<T>>::from_request_parts(parts, state).await { if let Some(value) = parts.extensions.get::<CachedEntry<T>>() {
Ok(Extension(CachedEntry(value))) => Ok(Self(value)), Ok(Self(value.0.clone()))
Err(_) => { } else {
let value = T::from_request_parts(parts, state).await?; let value = T::from_request_parts(parts, state).await?;
parts.extensions.insert(CachedEntry(value.clone())); parts.extensions.insert(CachedEntry(value.clone()));
Ok(Self(value)) Ok(Self(value))
}
} }
} }
} }