actix / actix-web

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

Home Page:https://actix.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Seems like web::Json can't deserilize json with nested keys as payload inside requests into rust structs

polarove opened this issue · comments

Your issue may already be reported! Please search on the Actix Web issue tracker before creating one.

Expected Behavior

Here is a json

{
    "name": "foo",
    "bar": {
            "foo": "bar"
     }
}

This is the defination of the api

#[derive(Serialize, Deserialize, Debug)]
struct Foobar{
    foo: String
}

#[derive(Serialize, Deserialize, Debug)]
struct Foo {
    name: String,
    bar: Foobar
}

use actix_web::{post, web::Json, Error, HttpResponse};
#[post("/test")]
async fn test(req: Json<Foo>) -> Result<HttpResponse, Error> {
    println!("{:?}", req);
    Ok(HttpResponse::Ok().body("hi"))
}

web::Json can't deserilize json with nested keys as payload inside requests into rust structs, and resolve the request as 400 (Bad request)

Current Behavior

Unable to deal with nested keys of json file

Possible Solution

Steps to Reproduce (for bugs)

Context

Your Environment

  • Rust Version (I.e, output of rustc -V): rustc 1.77.1 (7cf61ebde 2024-03-27)
  • Actix Web Version: 4.5.1