Skip to content

fix: #530 #531

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 2 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,14 @@ private IList GetTrackedManyRelationshipValue(IEnumerable<IIdentifiable> relatio
{
if (relationshipValueList == null) return null;
bool _wasAlreadyAttached = false;
Type entityType = null;
/// if we're not using entity resource separation, we can just read off the related type
/// from the RelationshipAttribute. If we DO use separation, RelationshipAttribute.DependentType
/// will point to the Resource, not the Entity, which is not the one we need here.
bool entityResourceSeparation = relationshipAttr.EntityPropertyName != null;
Type entityType = entityResourceSeparation ? null : relationshipAttr.DependentType;
var trackedPointerCollection = relationshipValueList.Select(pointer =>
{
/// todo: we can't just use relationshipAttr.Type because
/// todo: we can't just use relationshipAttr.DependentType because
/// this will point to the Resource type in the case of entity resource
/// separation. We should consider to store entity type on
/// the relationship attribute too.
Expand Down
1 change: 0 additions & 1 deletion src/JsonApiDotNetCore/Services/EntityResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ public virtual async Task<TResource> UpdateAsync(TId id, TResource resource)
_hookExecutor.AfterUpdate(AsList(entity), ResourcePipeline.Patch);
entity = _hookExecutor.OnReturn(AsList(entity), ResourcePipeline.Patch).SingleOrDefault();
}

return MapOut(entity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public async Task Can_Update_ToOne_Relationship_ThroughLink()
}

[Fact]
public async Task Can_Delete_Relationship_By_Patching_Resource()
public async Task Can_Delete_ToOne_Relationship_By_Patching_Resource()
{
// arrange
var person = _personFaker.Generate();
Expand Down Expand Up @@ -580,6 +580,64 @@ public async Task Can_Delete_Relationship_By_Patching_Resource()
Assert.Null(todoItemResult.Owner);
}


[Fact]
public async Task Can_Delete_ToMany_Relationship_By_Patching_Resource()
{
// arrange
var person = _personFaker.Generate();
var todoItem = _todoItemFaker.Generate();
person.TodoItems = new List<TodoItem>() { todoItem };
_context.People.Add(person);
_context.SaveChanges();

var builder = new WebHostBuilder()
.UseStartup<Startup>();

var server = new TestServer(builder);
var client = server.CreateClient();

var content = new
{
data = new
{
id = person.Id,
type = "people",
relationships = new Dictionary<string, object>
{
{ "todo-items", new
{
data = new List<object>
{

}
}
}
}
}
};

var httpMethod = new HttpMethod("PATCH");
var route = $"/api/v1/people/{person.Id}";
var request = new HttpRequestMessage(httpMethod, route);

string serializedContent = JsonConvert.SerializeObject(content);
request.Content = new StringContent(serializedContent);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");

// Act
var response = await _fixture.Client.SendAsync(request);

// Assert
var personResult = _context.People
.AsNoTracking()
.Include(p => p.TodoItems)
.Single(p => p.Id == person.Id);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Empty(personResult.TodoItems);
}

[Fact]
public async Task Can_Delete_Relationship_By_Patching_Relationship()
{
Expand Down