Skip to content

Update one-to-many through PATCH on resource not working #492

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
maurei opened this issue Apr 11, 2019 · 1 comment · Fixed by #493
Closed

Update one-to-many through PATCH on resource not working #492

maurei opened this issue Apr 11, 2019 · 1 comment · Fixed by #493

Comments

@maurei
Copy link
Member

maurei commented Apr 11, 2019

A core feature of the spec is currently not supported:

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

{
  "data": {
    "type": "authors",
    "id": "1",
    "relationships": {
      "books": {
        "data": [
          { "type": "books", "id": "2" },
          { "type": "books", "id": "3" }
        ]
      }
    }
  }
}

Cause of the problem
Because of incomplete information on _jsonApiContext in the case of one-to-many, which is set in the JsonApiDeserializer, the DefaultEntityRepository does not attempt to update such relationships. Details:

  • In the case of one-to-one, the JsonApiDeserializer adds meta info about this to _jsonApiContext.RelationshipsToUpdate right here in the SetHasOneRelationship method.
  • This property however is not updated in the case of one-to-many: in the SetHasManyRelationship method, only _jsonApiContext.HasManyRelationshipPointers are updated, which is not enough.
  • this is where the repository would be updating one-to-many relationships if they were included in _jsonApiContext.RelationshipsToUpdate.
  • Also in the repo: AttachRelationships(oldEntity) is fired before relationship.Key.SetValue(oldEntity, relationship.Value); is called, so any updates to the relations are currently not tracked.

A complication: the request above should perform a complete replace. This means that before updating the Author.BookId to its new value, the foreign keys of all previous Books for that author should be set to null (and an error should be thrown it is non-nullable).

Solution

  • Add a hasManyAttr value to the RelationshipsToUpdate dictionary in the JsonApiDeserializer right here.
  • in the case of a complete replace, include the related entities first see this topic; same for EF Core
  • do AttachRelationships(oldEntity) before actually setting the new relationship collection.

Note: the same issue exists for many-to-many, but it requires a different fix. Will address this in a separate issue.

Environment

  • JsonApiDotNetCore Version: latest
@maurei maurei changed the title Update to many with PATCH on resource not working Update one-tomany through PATCH on resource not working Apr 11, 2019
@maurei maurei changed the title Update one-tomany through PATCH on resource not working Update one-to-many through PATCH on resource not working Apr 11, 2019
maurei added a commit to wisepotato/JsonApiDotNetCore that referenced this issue Apr 11, 2019
maurei added a commit to wisepotato/JsonApiDotNetCore that referenced this issue Apr 11, 2019
maurei added a commit to wisepotato/JsonApiDotNetCore that referenced this issue Apr 15, 2019
@maurei
Copy link
Member Author

maurei commented Apr 15, 2019

There was an additional issue related to entity tracking that caused an error when performing a complete replacement with a new collection that contained an element that was already in the
previous collection. This has been addressed in #496.

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

Successfully merging a pull request may close this issue.

1 participant