Skip to content

Commit 5e330a8

Browse files
committed
feat(#494): proper complete replace
1 parent 19a8935 commit 5e330a8

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,23 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
298298

299299
if (_jsonApiContext.RelationshipsToUpdate.Any())
300300
{
301-
AttachRelationships(oldEntity);
302301
foreach (var relationship in _jsonApiContext.RelationshipsToUpdate)
303302
{
304-
/// If we are updating to-many relations from PATCH, we need to include the relation first,
305-
/// else it will not peform a complete replacement, as required by the specs.
306-
/// Also, we currently do not support the same for many-to-many
303+
307304
if (relationship.Key is HasManyAttribute && !(relationship.Key is HasManyThroughAttribute))
308-
await _context.Entry(oldEntity).Collection(relationship.Key.InternalRelationshipName).LoadAsync();
309-
relationship.Key.SetValue(oldEntity, relationship.Value); // article.tags = nieuwe lijst
305+
{
306+
/// If we are updating to-many relations from PATCH, we need to include the relation first,
307+
/// else it will not peform a complete replacement, as required by the specs.
308+
relationship.Key.SetValue(oldEntity, relationship.Value);
309+
} else if (relationship.Key is HasManyThroughAttribute throughAttribute)
310+
{
311+
// If we're updating many-to-many, we only have to load the ArticleTags.
312+
// The new value was already set in the AttachRelationships(oldEntity) call.
313+
// @TODO: It it not consistent that for many-to-many, the new relation value
314+
// is assigned in a helper function, whereas for one-to-many, it happens here.
315+
await _context.Entry(oldEntity).Collection(throughAttribute.InternalThroughName).LoadAsync();
316+
AttachRelationships(oldEntity);
317+
}
310318
}
311319
}
312320
await _context.SaveChangesAsync();

0 commit comments

Comments
 (0)