Skip to content

Posting JSONAPI data not binding #237

Closed
@wayne-o

Description

@wayne-o

When I have the following classes:

public class ApplicationUserViewModel : Identifiable<string>
    {
        public ApplicationUserViewModel()
        {
            Id = Guid.NewGuid().ToString();
            Roles = new List<ApplicationRoleViewModel>();
        }

        [NotMapped]
        public string StringId { get => Id; set => Id = value; }

        [Attr("username")] public string UserName { get; set; }
        [Attr("email")] public string Email { get; set; }

        [HasMany("roles")]
        public List<ApplicationRoleViewModel> Roles
        {
            get;
            set;
        }
    }

public class ApplicationRoleViewModel : Identifiable<string>
    {
        public ApplicationRoleViewModel()
        {
            Users = new List<ApplicationUserViewModel>();
            Id = Guid.NewGuid().ToString();
        }

        [NotMapped]
        public string StringId { get => Id; set => Id = value; }

        [Attr("name")]
        public string Name { get; set; }

        [HasMany("users")]
        public List<ApplicationUserViewModel> Users
        {
            get;
            set;
        }
    }

And I post the following JSON

{
  "data": {
    "id": "461a2700-114b-4667-bacf-e4425d52354f",
    "attributes": {
      "email": "123@123.com"
    },
    "relationships": {
      "roles": {
        "data": [
          {
            "type": "roles",
            "id": "67fd9442-9d8c-45de-bf42-4af8be9a8d40"
          }
        ]
      }
    },
    "type": "users"
  }
}

It doesn't deserialize properly and the model passed into the controller has empty properties.

Is there anything glaringly wrong here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions