From 0c8196f8b07022e56fb4f967c26426cd3076ffdf Mon Sep 17 00:00:00 2001 From: Logan Magee Date: Mon, 18 Jan 2021 03:14:29 -0900 Subject: [PATCH] Remove HttpResponseBuilder::json2() (#1903) It's not necessary to keep both json() and json2() around since the former reduces the ownership of its parameter to a borrow only to pass the reference to the latter. Users can instead borrow themselves when passing an owned value: there doesn't need to be two separate functions. This change also makes HttpResponseBuilder::json() take T: Deref so it can accept both references and web extractors like web::Json. --- actix-http/CHANGES.md | 3 +++ actix-http/src/response.rs | 37 ++++++++----------------------------- src/test.rs | 20 ++++++-------------- 3 files changed, 17 insertions(+), 43 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index c535b9866..34de7727a 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -14,6 +14,8 @@ * Renamed `IntoHeaderValue::{try_into => try_into_value}` to avoid ambiguity with std `TryInto` trait. [#1894] * `Extensions::insert` returns Option of replaced item. [#1904] +* Remove `HttpResponseBuilder::json2()` and make `HttpResponseBuilder::json()` take a value by + reference. [#1903] ### Removed * `ResponseBuilder::set`; use `ResponseBuilder::insert_header`. [#1869] @@ -24,6 +26,7 @@ [#1869]: https://github.com/actix/actix-web/pull/1869 [#1894]: https://github.com/actix/actix-web/pull/1894 +[#1903]: https://github.com/actix/actix-web/pull/1903 [#1904]: https://github.com/actix/actix-web/pull/1904 [#1912]: https://github.com/actix/actix-web/pull/1912 diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index 880f93c6a..110514e05 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -5,6 +5,7 @@ use std::{ convert::TryInto, fmt, future::Future, + ops, pin::Pin, str, task::{Context, Poll}, @@ -651,19 +652,15 @@ impl ResponseBuilder { self.body(Body::from_message(BodyStream::new(stream))) } - #[inline] /// Set a json body and generate `Response` /// /// `ResponseBuilder` can not be used after this call. - pub fn json(&mut self, value: T) -> Response { - self.json2(&value) - } - - /// Set a json body and generate `Response` - /// - /// `ResponseBuilder` can not be used after this call. - pub fn json2(&mut self, value: &T) -> Response { - match serde_json::to_string(value) { + pub fn json(&mut self, value: T) -> Response + where + T: ops::Deref, + T::Target: Serialize, + { + match serde_json::to_string(&*value) { Ok(body) => { let contains = if let Some(parts) = parts(&mut self.head, &self.err) { parts.headers.contains_key(header::CONTENT_TYPE) @@ -979,25 +976,7 @@ mod tests { fn test_json_ct() { let resp = Response::build(StatusCode::OK) .insert_header((CONTENT_TYPE, "text/json")) - .json(vec!["v1", "v2", "v3"]); - let ct = resp.headers().get(CONTENT_TYPE).unwrap(); - assert_eq!(ct, HeaderValue::from_static("text/json")); - assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]"); - } - - #[test] - fn test_json2() { - let resp = Response::build(StatusCode::OK).json2(&vec!["v1", "v2", "v3"]); - let ct = resp.headers().get(CONTENT_TYPE).unwrap(); - assert_eq!(ct, HeaderValue::from_static("application/json")); - assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]"); - } - - #[test] - fn test_json2_ct() { - let resp = Response::build(StatusCode::OK) - .insert_header((CONTENT_TYPE, "text/json")) - .json2(&vec!["v1", "v2", "v3"]); + .json(&vec!["v1", "v2", "v3"]); let ct = resp.headers().get(CONTENT_TYPE).unwrap(); assert_eq!(ct, HeaderValue::from_static("text/json")); assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]"); diff --git a/src/test.rs b/src/test.rs index 55b436084..769cece55 100644 --- a/src/test.rs +++ b/src/test.rs @@ -239,7 +239,7 @@ where /// web::resource("/people") /// .route(web::post().to(|person: web::Json| async { /// HttpResponse::Ok() -/// .json(person.into_inner())}) +/// .json(person)}) /// )) /// ).await; /// @@ -299,7 +299,7 @@ where /// web::resource("/people") /// .route(web::post().to(|person: web::Json| async { /// HttpResponse::Ok() -/// .json(person.into_inner())}) +/// .json(person)}) /// )) /// ).await; /// @@ -1121,9 +1121,7 @@ mod tests { #[actix_rt::test] async fn test_response_json() { let mut app = init_service(App::new().service(web::resource("/people").route( - web::post().to(|person: web::Json| { - HttpResponse::Ok().json(person.into_inner()) - }), + web::post().to(|person: web::Json| HttpResponse::Ok().json(person)), ))) .await; @@ -1142,9 +1140,7 @@ mod tests { #[actix_rt::test] async fn test_body_json() { let mut app = init_service(App::new().service(web::resource("/people").route( - web::post().to(|person: web::Json| { - HttpResponse::Ok().json(person.into_inner()) - }), + web::post().to(|person: web::Json| HttpResponse::Ok().json(person)), ))) .await; @@ -1164,9 +1160,7 @@ mod tests { #[actix_rt::test] async fn test_request_response_form() { let mut app = init_service(App::new().service(web::resource("/people").route( - web::post().to(|person: web::Form| { - HttpResponse::Ok().json(person.into_inner()) - }), + web::post().to(|person: web::Form| HttpResponse::Ok().json(person)), ))) .await; @@ -1190,9 +1184,7 @@ mod tests { #[actix_rt::test] async fn test_request_response_json() { let mut app = init_service(App::new().service(web::resource("/people").route( - web::post().to(|person: web::Json| { - HttpResponse::Ok().json(person.into_inner()) - }), + web::post().to(|person: web::Json| HttpResponse::Ok().json(person)), ))) .await;