Problem passing list attribute to HttpPost function

Viewed 30

This is a test function from my controller:

    [Route("TestingCurrentFunction")]
    [HttpPost]
    public IActionResult FunctionTesting([FromBody] User user)
    {

        return Ok(user);
    }

When I call it with this JSON body:

{
   "name": "Igor",
   "username": "igor123",
   "password": "igrotheking123",
   "email": "igor@user.com",
   "tags": [
     {
        "genre": "superhero"
     }
   ]
}

it returns that it cannot read "tags[0].genre" and that genre field is required, but obviously there is genre. The class User in backend has a list of Tag objects, but Swagger detected them and generated this JSON body, I just filled it with random values. Why can't the function read tags?

Code for the User

using ProjekatN4J.DomainModel;
namespace ProjekatN4J
{
public class User
{
    public string name { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public string email { get; set; }

    public List<Tag> tags { get; set; }

    public User(string ime, string korIme, string sifra, string mejl)
    {
        this.name = ime;
        this.username = korIme;
        this.password = sifra;
        this.email = mejl;
        this.tags = new List<Tag>();
    }

    public User() 
    {
        this.tags = new List<Tag>();
    }
}

}

And for Tag:

namespace ProjekatN4J.DomainModel
{
public class Tag
{
    public string genre { get; }

    public Tag(string genre)
    {
        this.genre = genre;
    }

    public Tag()
    {

    }
   }
 }
0 Answers
Related