mirror of
https://github.com/actix/actix-web.git
synced 2024-11-26 11:31:09 +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:
parent
ee10148444
commit
0c8196f8b0
3 changed files with 17 additions and 43 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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<T: Serialize>(&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<T: Serialize>(&mut self, value: &T) -> Response {
|
||||
match serde_json::to_string(value) {
|
||||
pub fn json<T>(&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\"]");
|
||||
|
|
20
src/test.rs
20
src/test.rs
|
@ -239,7 +239,7 @@ where
|
|||
/// web::resource("/people")
|
||||
/// .route(web::post().to(|person: web::Json<Person>| 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<Person>| 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<Person>| {
|
||||
HttpResponse::Ok().json(person.into_inner())
|
||||
}),
|
||||
web::post().to(|person: web::Json<Person>| 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<Person>| {
|
||||
HttpResponse::Ok().json(person.into_inner())
|
||||
}),
|
||||
web::post().to(|person: web::Json<Person>| 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<Person>| {
|
||||
HttpResponse::Ok().json(person.into_inner())
|
||||
}),
|
||||
web::post().to(|person: web::Form<Person>| 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<Person>| {
|
||||
HttpResponse::Ok().json(person.into_inner())
|
||||
}),
|
||||
web::post().to(|person: web::Json<Person>| HttpResponse::Ok().json(person)),
|
||||
)))
|
||||
.await;
|
||||
|
||||
|
|
Loading…
Reference in a new issue