1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-11 11:21:15 +00:00

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.
This commit is contained in:
Logan Magee 2021-01-18 03:14:29 -09:00 committed by GitHub
parent ee10148444
commit 0c8196f8b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 43 deletions

View file

@ -14,6 +14,8 @@
* Renamed `IntoHeaderValue::{try_into => try_into_value}` to avoid ambiguity with std * Renamed `IntoHeaderValue::{try_into => try_into_value}` to avoid ambiguity with std
`TryInto` trait. [#1894] `TryInto` trait. [#1894]
* `Extensions::insert` returns Option of replaced item. [#1904] * `Extensions::insert` returns Option of replaced item. [#1904]
* Remove `HttpResponseBuilder::json2()` and make `HttpResponseBuilder::json()` take a value by
reference. [#1903]
### Removed ### Removed
* `ResponseBuilder::set`; use `ResponseBuilder::insert_header`. [#1869] * `ResponseBuilder::set`; use `ResponseBuilder::insert_header`. [#1869]
@ -24,6 +26,7 @@
[#1869]: https://github.com/actix/actix-web/pull/1869 [#1869]: https://github.com/actix/actix-web/pull/1869
[#1894]: https://github.com/actix/actix-web/pull/1894 [#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 [#1904]: https://github.com/actix/actix-web/pull/1904
[#1912]: https://github.com/actix/actix-web/pull/1912 [#1912]: https://github.com/actix/actix-web/pull/1912

View file

@ -5,6 +5,7 @@ use std::{
convert::TryInto, convert::TryInto,
fmt, fmt,
future::Future, future::Future,
ops,
pin::Pin, pin::Pin,
str, str,
task::{Context, Poll}, task::{Context, Poll},
@ -651,19 +652,15 @@ impl ResponseBuilder {
self.body(Body::from_message(BodyStream::new(stream))) self.body(Body::from_message(BodyStream::new(stream)))
} }
#[inline]
/// Set a json body and generate `Response` /// Set a json body and generate `Response`
/// ///
/// `ResponseBuilder` can not be used after this call. /// `ResponseBuilder` can not be used after this call.
pub fn json<T: Serialize>(&mut self, value: T) -> Response { pub fn json<T>(&mut self, value: T) -> Response
self.json2(&value) where
} T: ops::Deref,
T::Target: Serialize,
/// Set a json body and generate `Response` {
/// match serde_json::to_string(&*value) {
/// `ResponseBuilder` can not be used after this call.
pub fn json2<T: Serialize>(&mut self, value: &T) -> Response {
match serde_json::to_string(value) {
Ok(body) => { Ok(body) => {
let contains = if let Some(parts) = parts(&mut self.head, &self.err) { let contains = if let Some(parts) = parts(&mut self.head, &self.err) {
parts.headers.contains_key(header::CONTENT_TYPE) parts.headers.contains_key(header::CONTENT_TYPE)
@ -979,25 +976,7 @@ mod tests {
fn test_json_ct() { fn test_json_ct() {
let resp = Response::build(StatusCode::OK) let resp = Response::build(StatusCode::OK)
.insert_header((CONTENT_TYPE, "text/json")) .insert_header((CONTENT_TYPE, "text/json"))
.json(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\"]");
}
#[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"]);
let ct = resp.headers().get(CONTENT_TYPE).unwrap(); let ct = resp.headers().get(CONTENT_TYPE).unwrap();
assert_eq!(ct, HeaderValue::from_static("text/json")); assert_eq!(ct, HeaderValue::from_static("text/json"));
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]"); assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");

View file

@ -239,7 +239,7 @@ where
/// web::resource("/people") /// web::resource("/people")
/// .route(web::post().to(|person: web::Json<Person>| async { /// .route(web::post().to(|person: web::Json<Person>| async {
/// HttpResponse::Ok() /// HttpResponse::Ok()
/// .json(person.into_inner())}) /// .json(person)})
/// )) /// ))
/// ).await; /// ).await;
/// ///
@ -299,7 +299,7 @@ where
/// web::resource("/people") /// web::resource("/people")
/// .route(web::post().to(|person: web::Json<Person>| async { /// .route(web::post().to(|person: web::Json<Person>| async {
/// HttpResponse::Ok() /// HttpResponse::Ok()
/// .json(person.into_inner())}) /// .json(person)})
/// )) /// ))
/// ).await; /// ).await;
/// ///
@ -1121,9 +1121,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_response_json() { async fn test_response_json() {
let mut app = init_service(App::new().service(web::resource("/people").route( let mut app = init_service(App::new().service(web::resource("/people").route(
web::post().to(|person: web::Json<Person>| { web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
HttpResponse::Ok().json(person.into_inner())
}),
))) )))
.await; .await;
@ -1142,9 +1140,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_body_json() { async fn test_body_json() {
let mut app = init_service(App::new().service(web::resource("/people").route( let mut app = init_service(App::new().service(web::resource("/people").route(
web::post().to(|person: web::Json<Person>| { web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
HttpResponse::Ok().json(person.into_inner())
}),
))) )))
.await; .await;
@ -1164,9 +1160,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_request_response_form() { async fn test_request_response_form() {
let mut app = init_service(App::new().service(web::resource("/people").route( let mut app = init_service(App::new().service(web::resource("/people").route(
web::post().to(|person: web::Form<Person>| { web::post().to(|person: web::Form<Person>| HttpResponse::Ok().json(person)),
HttpResponse::Ok().json(person.into_inner())
}),
))) )))
.await; .await;
@ -1190,9 +1184,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_request_response_json() { async fn test_request_response_json() {
let mut app = init_service(App::new().service(web::resource("/people").route( let mut app = init_service(App::new().service(web::resource("/people").route(
web::post().to(|person: web::Json<Person>| { web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
HttpResponse::Ok().json(person.into_inner())
}),
))) )))
.await; .await;