Skip to content

Commit 9aea5de

Browse files
authored
Merge pull request #531 from json-api-dotnet/fix/#530
fix: #530
2 parents d490b8b + 2691dea commit 9aea5de

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,14 @@ private IList GetTrackedManyRelationshipValue(IEnumerable<IIdentifiable> relatio
326326
{
327327
if (relationshipValueList == null) return null;
328328
bool _wasAlreadyAttached = false;
329-
Type entityType = null;
329+
/// if we're not using entity resource separation, we can just read off the related type
330+
/// from the RelationshipAttribute. If we DO use separation, RelationshipAttribute.DependentType
331+
/// will point to the Resource, not the Entity, which is not the one we need here.
332+
bool entityResourceSeparation = relationshipAttr.EntityPropertyName != null;
333+
Type entityType = entityResourceSeparation ? null : relationshipAttr.DependentType;
330334
var trackedPointerCollection = relationshipValueList.Select(pointer =>
331335
{
332-
/// todo: we can't just use relationshipAttr.Type because
336+
/// todo: we can't just use relationshipAttr.DependentType because
333337
/// this will point to the Resource type in the case of entity resource
334338
/// separation. We should consider to store entity type on
335339
/// the relationship attribute too.

src/JsonApiDotNetCore/Services/EntityResourceService.cs

-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ public virtual async Task<TResource> UpdateAsync(TId id, TResource resource)
220220
_hookExecutor.AfterUpdate(AsList(entity), ResourcePipeline.Patch);
221221
entity = _hookExecutor.OnReturn(AsList(entity), ResourcePipeline.Patch).SingleOrDefault();
222222
}
223-
224223
return MapOut(entity);
225224
}
226225

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingRelationshipsTests.cs

+59-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public async Task Can_Update_ToOne_Relationship_ThroughLink()
528528
}
529529

530530
[Fact]
531-
public async Task Can_Delete_Relationship_By_Patching_Resource()
531+
public async Task Can_Delete_ToOne_Relationship_By_Patching_Resource()
532532
{
533533
// arrange
534534
var person = _personFaker.Generate();
@@ -580,6 +580,64 @@ public async Task Can_Delete_Relationship_By_Patching_Resource()
580580
Assert.Null(todoItemResult.Owner);
581581
}
582582

583+
584+
[Fact]
585+
public async Task Can_Delete_ToMany_Relationship_By_Patching_Resource()
586+
{
587+
// arrange
588+
var person = _personFaker.Generate();
589+
var todoItem = _todoItemFaker.Generate();
590+
person.TodoItems = new List<TodoItem>() { todoItem };
591+
_context.People.Add(person);
592+
_context.SaveChanges();
593+
594+
var builder = new WebHostBuilder()
595+
.UseStartup<Startup>();
596+
597+
var server = new TestServer(builder);
598+
var client = server.CreateClient();
599+
600+
var content = new
601+
{
602+
data = new
603+
{
604+
id = person.Id,
605+
type = "people",
606+
relationships = new Dictionary<string, object>
607+
{
608+
{ "todo-items", new
609+
{
610+
data = new List<object>
611+
{
612+
613+
}
614+
}
615+
}
616+
}
617+
}
618+
};
619+
620+
var httpMethod = new HttpMethod("PATCH");
621+
var route = $"/api/v1/people/{person.Id}";
622+
var request = new HttpRequestMessage(httpMethod, route);
623+
624+
string serializedContent = JsonConvert.SerializeObject(content);
625+
request.Content = new StringContent(serializedContent);
626+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
627+
628+
// Act
629+
var response = await _fixture.Client.SendAsync(request);
630+
631+
// Assert
632+
var personResult = _context.People
633+
.AsNoTracking()
634+
.Include(p => p.TodoItems)
635+
.Single(p => p.Id == person.Id);
636+
637+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
638+
Assert.Empty(personResult.TodoItems);
639+
}
640+
583641
[Fact]
584642
public async Task Can_Delete_Relationship_By_Patching_Relationship()
585643
{

0 commit comments

Comments
 (0)