Skip to content

PATCH request with HasManyThrough relationship doesn't perform full replacement #459

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

Closed
XaserAcheron opened this issue Dec 12, 2018 · 2 comments
Labels

Comments

@XaserAcheron
Copy link
Contributor

Description

I'm running into a case where PATCHing a record with a HasManyThrough relationship is failing to do a complete replacement, as per spec. Consider the classic example:

PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
    "type": "articles",
    "id": "1",
    "relationships": {
      "tags": {
        "data": [
          { "type": "tags", "id": "2" },
          { "type": "tags", "id": "3" }
        ]
      }
    }
  }
}

For whatever reason, it's not deleting the existing ArticleTag records before trying to insert new ones, e.g. If I already have tag#2 assigned to article#1, it tries to re-insert the record, leading to a primary key violation in the ArticleTag table.

For the record, it works just fine if issuing a PATCH to articles/1/relationships/tags, like so:

PATCH /articles/1/relationships/tags HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": [
    { "type": "tags", "id": "2" },
    { "type": "tags", "id": "3" }
  ]
}

...but ember-data really really wants to submit things all as one request, so here we are. :P

Poking around the source code a bit, looks there's a whole bunch of handling for this case in GenericProcessor that only seems to get used when entering the controller via PatchRelationshipsAsync -- i.e. if you start from PatchAsync, that workflow never handles it.

Having said that, there's a test here that looks like it should catch this -- I'm not totally sure why it works but my case doesn't just yet.

I'll see if I can do more spelunking soonish. Gotta get Docker to behave first. :P

Environment

  • JsonApiDotNetCore Version: 3.1.0-alpha3
  • Other Relevant Package Versions: N/A
@XaserAcheron
Copy link
Contributor Author

Reproduced & committed a failing test on this branch.

The gist: the existing m2m PATCH test creates an Article and a Tag in the database before running, but doesn't create any ArticleTags, so the PATCH is dealing with a blank slate. I created a second test (linked above) where the database has an existing ArticleTag, and sure enough, the PATCH fails with a duplicate primary key error.

No solution just yet, but we're one step closer.

@maurei
Copy link
Member

maurei commented Apr 24, 2019

Addressed in #494. Will be fixed in v3.2.0

@maurei maurei closed this as completed Apr 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants