From 19f6252790051279c7e091d026bac2c8dd8c1e52 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 19 Nov 2023 16:31:20 +0100 Subject: [PATCH] Implement `FromRequestParts` for `Parts` and `Extensions` Fixes https://github.com/tokio-rs/axum/issues/2320 --- axum-core/src/extract/request_parts.rs | 20 ++++++++++++++++---- axum/CHANGELOG.md | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/axum-core/src/extract/request_parts.rs b/axum-core/src/extract/request_parts.rs index 1950861513..c9a48e859f 100644 --- a/axum-core/src/extract/request_parts.rs +++ b/axum-core/src/extract/request_parts.rs @@ -2,7 +2,7 @@ use super::{rejection::*, FromRequest, FromRequestParts, Request}; use crate::{body::Body, RequestExt}; use async_trait::async_trait; use bytes::Bytes; -use http::{request::Parts, HeaderMap, Method, Uri, Version}; +use http::{request::Parts, Extensions, HeaderMap, Method, Uri, Version}; use http_body_util::BodyExt; use std::convert::Infallible; @@ -115,14 +115,26 @@ where } #[async_trait] -impl FromRequest for Parts +impl FromRequestParts for Parts where S: Send + Sync, { type Rejection = Infallible; - async fn from_request(req: Request, _: &S) -> Result { - Ok(req.into_parts().0) + async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + Ok(parts.clone()) + } +} + +#[async_trait] +impl FromRequestParts for Extensions +where + S: Send + Sync, +{ + type Rejection = Infallible; + + async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + Ok(parts.extensions.clone()) } } diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index b561dd8ac8..9195b0ff76 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -69,6 +69,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **breaking:** `impl IntoResponse(Parts) for Extension` now requires `T: Clone`, as that is required by the http crate ([#1882]) - **added:** Add `axum::Json::from_bytes` ([#2244]) +- **added:** Implement `FromRequestParts` for `http::request::Parts` ([#2328]) +- **added:** Implement `FromRequestParts` for `http::Extensions` ([#2328]) [#1664]: https://github.com/tokio-rs/axum/pull/1664 [#1751]: https://github.com/tokio-rs/axum/pull/1751 @@ -91,6 +93,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#2143]: https://github.com/tokio-rs/axum/pull/2143 [#2149]: https://github.com/tokio-rs/axum/pull/2149 [#2244]: https://github.com/tokio-rs/axum/pull/2244 +[#2328]: https://github.com/tokio-rs/axum/pull/2328 # 0.6.17 (25. April, 2023)