Skip to content

Feat/#494 #496

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

Merged
merged 12 commits into from
Apr 25, 2019
Prev Previous commit
Next Next commit
fix(#494): passing previous tests
  • Loading branch information
maurei committed Apr 15, 2019
commit 457781c91de6b186de2bcb355c88926251992cc3
33 changes: 21 additions & 12 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,31 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)

if (_jsonApiContext.RelationshipsToUpdate.Any())
{
/// For one-to-many and many-to-many, the PATCH must perform a
/// complete replace. When assigning new relationship values,
/// it will only be like this if the to-be-replaced entities are loaded
foreach (var relationship in _jsonApiContext.RelationshipsToUpdate)
{

if (relationship.Key is HasManyAttribute && !(relationship.Key is HasManyThroughAttribute))
{
/// If we are updating to-many relations from PATCH, we need to include the relation first,
/// else it will not peform a complete replacement, as required by the specs.
relationship.Key.SetValue(oldEntity, relationship.Value);
} else if (relationship.Key is HasManyThroughAttribute throughAttribute)
if (relationship.Key is HasManyThroughAttribute throughAttribute)
{
// If we're updating many-to-many, we only have to load the ArticleTags.
// The new value was already set in the AttachRelationships(oldEntity) call.
// @TODO: It it not consistent that for many-to-many, the new relation value
// is assigned in a helper function, whereas for one-to-many, it happens here.
await _context.Entry(oldEntity).Collection(throughAttribute.InternalThroughName).LoadAsync();
AttachRelationships(oldEntity);
}
else if (relationship.Key is HasManyAttribute)
{
await _context.Entry(oldEntity).Collection(relationship.Key.InternalRelationshipName).LoadAsync();
}
}
AttachRelationships(oldEntity);

/// @TODO: It it not consistent that for many-to-many, the new relationship value
/// is assigned in AttachRelationships() helperfunction, whereas for
/// one-to-many and one-to-one, we need to do it manually as below.
/// As a result, we need to loop over RelationshipsToUpdate a second time.
foreach (var relationship in _jsonApiContext.RelationshipsToUpdate)
{
if (!(relationship.Key is HasManyThroughAttribute))
{
relationship.Key.SetValue(oldEntity, relationship.Value);
}
}
}
Expand Down