Skip to content

Relations being validated on POST #671

Closed
@Lobosque

Description

@Lobosque

Description

I am trying to POST an enterprise resource. while posting it, I need to associate it to a partner:

{
  "data": {
    "type": "enterprise",
    "attributes": {
      "name": "Ember",
      "alias": "Emba",
      "documentNumber": "1234567"
    },
    "relationships": {
      "partner": {
        "data": { "type": "partner", "id": 10 }
      },
    }
  }
}

And these are the models:

    [Resource("enterprise")]
    public class Enterprise : Identifiable, IEntity
    {
        [Column("EnterpriseId")]
        public override int Id { get; set; }

        [Required]
        [Attr]
        public string Name  { get; set; }

        [Required]
        [Attr]
        public string Alias { get; set; }

        [Required]
        [Attr]
        public string DocumentNumber { get; set; }

        public int PartnerId { get; set; }
        [HasOne("partner")]
        public virtual EnterprisePartner Partner { get; set; }
    }

    [Resource("partner")]
    public class EnterprisePartner : Identifiable, IEntity
    {
        [Required]
        [Attr]
        public string Token { get; set; }

        [Required]
        [Attr]
        public string Name { get; set; }

        [Attr]
        public string Alias { get; set; }
    }

I am getting the following return:

{
    "errors": [
        {
            "title": "ArgumentNullException",
            "detail": "Value cannot be null. (Parameter 'element')",
            "status": "500"
        }
    ]
}

While debugging, I found that the partner attributes are being validated, and of course not passing since I am not sending any value:

[0] [string]:"Partner.Name"
[1] [string]:"Partner.Token"

I expect that while Posting, I am just associating the entities, not trying to create a new one. Should I be doing this in a different way?
The final error happens when the code tries to access Partner.Name attribute from Enterprise, but the actual problem seems to be the validation of the related resource.

Environment

  • JsonApiDotNetCore 4.0.0alpha

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions