Skip to content

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

Closed
@XaserAcheron

Description

@XaserAcheron

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

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