Closed
Description
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
, theJsonApiDeserializer
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 beforerelationship.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 Book
s for that author should be set to null (and an error should be thrown it is non-nullable).
Solution
- Add a
hasManyAttr
value to theRelationshipsToUpdate
dictionary in theJsonApiDeserializer
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
Metadata
Metadata
Assignees
Labels
No labels