Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relations being validated on POST #671

Closed
Lobosque opened this issue Jan 23, 2020 · 3 comments · Fixed by #831
Closed

Relations being validated on POST #671

Lobosque opened this issue Jan 23, 2020 · 3 comments · Fixed by #831
Assignees
Labels

Comments

@Lobosque
Copy link

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
@maurei
Copy link
Member

maurei commented Feb 12, 2020

Hi @Lobosque. If I'm understanding correctly you're trying to use create a resource and relate it to an already existing resource this part of the json:api spec, but the usage of .NET Core's [Required] attributes is messing up the pipeline?

I.e. Could you try removing the [Required] attributes in your EnterprisePartner class and see if that resolves the issue? If that does not resolve the issue, please reflect on this test which covers this part of the spec.

If the error indeed stems from mixing JADNC with [Required], then that is something I will need to look into, as we aim to support as much as possible all the standard .net core features next to what JADNC offers.

Thanks in advance for confirming!

@bart-degreed
Copy link
Contributor

I haven't tried, but this may have been fixed in #781, which is now in master. @Lobosque can you confirm?

@bart-degreed
Copy link
Contributor

Confirming that I can reproduce the problem and this is not fixed yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants