From 12f7720309a5bffc0ae93ece4b7a0462ed901f56 Mon Sep 17 00:00:00 2001
From: Rob Ede <robjtede@icloud.com>
Date: Tue, 22 Jun 2021 15:50:58 +0100
Subject: [PATCH] deprecate `App::data` and `App::data_factory` (#2271)

---
 CHANGES.md           |  2 ++
 src/app.rs           | 15 ++++++++++++---
 src/app_service.rs   |  2 ++
 src/data.rs          |  6 ++++++
 src/request.rs       |  2 ++
 src/resource.rs      |  5 +++++
 src/scope.rs         |  5 +++++
 src/service.rs       |  2 ++
 src/test.rs          |  2 ++
 src/types/payload.rs |  2 ++
 tests/test_server.rs |  2 ++
 11 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 17ae711d..fa4aa859 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -3,8 +3,10 @@
 ## Unreleased - 2021-xx-xx
 ### Changed
 * Change compression algorithm features flags. [#2250]
+* Deprecate `App::data` and `App::data_factory`. [#2271]
 
 [#2250]: https://github.com/actix/actix-web/pull/2250
+[#2271]: https://github.com/actix/actix-web/pull/2271
 
 
 ## 4.0.0-beta.7 - 2021-06-17
diff --git a/src/app.rs b/src/app.rs
index 357d45ee..8c622dd3 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -98,13 +98,18 @@ where
     ///         web::resource("/index.html").route(
     ///             web::get().to(index)));
     /// ```
+    #[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
     pub fn data<U: 'static>(self, data: U) -> Self {
         self.app_data(Data::new(data))
     }
 
-    /// Set application data factory. This function is
-    /// similar to `.data()` but it accepts data factory. Data object get
-    /// constructed asynchronously during application initialization.
+    /// Add application data factory. This function is similar to `.data()` but it accepts a
+    /// "data factory". Data values are constructed asynchronously during application
+    /// initialization, before the server starts accepting requests.
+    #[deprecated(
+        since = "4.0.0",
+        note = "Construct data value before starting server and use `.app_data(Data::new(val))` instead."
+    )]
     pub fn data_factory<F, Out, D, E>(mut self, data: F) -> Self
     where
         F: Fn() -> Out + 'static,
@@ -518,6 +523,8 @@ mod tests {
         assert_eq!(resp.status(), StatusCode::CREATED);
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_data_factory() {
         let srv = init_service(
@@ -541,6 +548,8 @@ mod tests {
         assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_data_factory_errors() {
         let srv = try_init_service(
diff --git a/src/app_service.rs b/src/app_service.rs
index ca6f3620..a9247b19 100644
--- a/src/app_service.rs
+++ b/src/app_service.rs
@@ -349,6 +349,8 @@ mod tests {
         }
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_drop_data() {
         let data = Arc::new(AtomicBool::new(false));
diff --git a/src/data.rs b/src/data.rs
index f09a8889..51db6ce4 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -154,6 +154,8 @@ mod tests {
         web, App, HttpResponse,
     };
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_data_extractor() {
         let srv = init_service(App::new().data("TEST".to_string()).service(
@@ -221,6 +223,8 @@ mod tests {
         assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_route_data_extractor() {
         let srv = init_service(
@@ -250,6 +254,8 @@ mod tests {
         assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_override_data() {
         let srv =
diff --git a/src/request.rs b/src/request.rs
index a364f8b1..bff66f08 100644
--- a/src/request.rs
+++ b/src/request.rs
@@ -711,6 +711,8 @@ mod tests {
         assert_eq!(body, Bytes::from_static(b"1"));
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_extensions_dropped() {
         struct Tracker {
diff --git a/src/resource.rs b/src/resource.rs
index 8c2b83b6..9455895e 100644
--- a/src/resource.rs
+++ b/src/resource.rs
@@ -196,6 +196,7 @@ where
     ///           ));
     /// }
     /// ```
+    #[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
     pub fn data<U: 'static>(self, data: U) -> Self {
         self.app_data(Data::new(data))
     }
@@ -694,6 +695,8 @@ mod tests {
         assert_eq!(resp.status(), StatusCode::NO_CONTENT);
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_data() {
         let srv = init_service(
@@ -726,6 +729,8 @@ mod tests {
         assert_eq!(resp.status(), StatusCode::OK);
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_data_default_service() {
         let srv = init_service(
diff --git a/src/scope.rs b/src/scope.rs
index 412c01d9..86304074 100644
--- a/src/scope.rs
+++ b/src/scope.rs
@@ -146,6 +146,7 @@ where
     ///     );
     /// }
     /// ```
+    #[deprecated(since = "4.0.0", note = "Use `.app_data(Data::new(val))` instead.")]
     pub fn data<U: 'static>(self, data: U) -> Self {
         self.app_data(Data::new(data))
     }
@@ -990,6 +991,8 @@ mod tests {
         );
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_override_data() {
         let srv = init_service(App::new().data(1usize).service(
@@ -1008,6 +1011,8 @@ mod tests {
         assert_eq!(resp.status(), StatusCode::OK);
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_override_data_default_service() {
         let srv = init_service(App::new().data(1usize).service(
diff --git a/src/service.rs b/src/service.rs
index e3e975ce..a772e20b 100644
--- a/src/service.rs
+++ b/src/service.rs
@@ -649,6 +649,8 @@ mod tests {
         assert_eq!(resp.status(), http::StatusCode::NOT_FOUND);
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_service_data() {
         let srv =
diff --git a/src/test.rs b/src/test.rs
index de97dc8a..05a4ba7f 100644
--- a/src/test.rs
+++ b/src/test.rs
@@ -839,6 +839,8 @@ mod tests {
         assert!(res.status().is_success());
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_server_data() {
         async fn handler(data: web::Data<usize>) -> impl Responder {
diff --git a/src/types/payload.rs b/src/types/payload.rs
index 3b0d1d6c..87378701 100644
--- a/src/types/payload.rs
+++ b/src/types/payload.rs
@@ -398,6 +398,8 @@ mod tests {
         assert!(cfg.check_mimetype(&req).is_ok());
     }
 
+    // allow deprecated App::data
+    #[allow(deprecated)]
     #[actix_rt::test]
     async fn test_config_recall_locations() {
         async fn bytes_handler(_: Bytes) -> impl Responder {
diff --git a/tests/test_server.rs b/tests/test_server.rs
index 520eb5ce..9131d1f2 100644
--- a/tests/test_server.rs
+++ b/tests/test_server.rs
@@ -1028,6 +1028,8 @@ async fn test_normalize() {
     assert!(response.status().is_success());
 }
 
+// allow deprecated App::data
+#[allow(deprecated)]
 #[actix_rt::test]
 async fn test_data_drop() {
     use std::sync::{