Skip to content

Included Data Should Only Contain Unique Entries #80

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
Mar 30, 2017
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
3 changes: 2 additions & 1 deletion src/JsonApiDotNetCore/Builders/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ private List<DocumentData> GetIncludedEntities(ContextEntity contextEntity, IIde
private void AddIncludedEntity(List<DocumentData> entities, IIdentifiable entity)
{
var includedEntity = GetIncludedEntity(entity);
if(includedEntity != null)

if(includedEntity != null && !entities.Any(doc => doc.Id == includedEntity.Id && doc.Type == includedEntity.Type))
entities.Add(includedEntity);
}

Expand Down
13 changes: 13 additions & 0 deletions src/JsonApiDotNetCoreExample/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{ }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TodoItem>()
.HasOne(t => t.Assignee)
.WithMany(p => p.AssignedTodoItems)
.HasForeignKey(t => t.AssigneeId);

modelBuilder.Entity<TodoItem>()
.HasOne(t => t.Owner)
.WithMany(p => p.TodoItems)
.HasForeignKey(t => t.OwnerId);
}

public DbSet<TodoItem> TodoItems { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0" />
<PackageReference Include="DotNetCoreDocs" Version="0.4.0" />
</ItemGroup>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;

namespace JsonApiDotNetCoreExample.Migrations
{
public partial class AddAssignedTodoItems : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "AssigneeId",
table: "TodoItems",
nullable: true);

migrationBuilder.CreateIndex(
name: "IX_TodoItems_AssigneeId",
table: "TodoItems",
column: "AssigneeId");

migrationBuilder.AddForeignKey(
name: "FK_TodoItems_People_AssigneeId",
table: "TodoItems",
column: "AssigneeId",
principalTable: "People",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_TodoItems_People_AssigneeId",
table: "TodoItems");

migrationBuilder.DropIndex(
name: "IX_TodoItems_AssigneeId",
table: "TodoItems");

migrationBuilder.DropColumn(
name: "AssigneeId",
table: "TodoItems");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<int?>("AssigneeId");

b.Property<Guid?>("CollectionId");

b.Property<string>("Description");
Expand All @@ -45,6 +47,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasKey("Id");

b.HasIndex("AssigneeId");

b.HasIndex("CollectionId");

b.HasIndex("OwnerId");
Expand All @@ -70,6 +74,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("JsonApiDotNetCoreExample.Models.TodoItem", b =>
{
b.HasOne("JsonApiDotNetCoreExample.Models.Person", "Assignee")
.WithMany("AssignedTodoItems")
.HasForeignKey("AssigneeId");

b.HasOne("JsonApiDotNetCoreExample.Models.TodoItemCollection", "Collection")
.WithMany("TodoItems")
.HasForeignKey("CollectionId");
Expand Down
3 changes: 3 additions & 0 deletions src/JsonApiDotNetCoreExample/Models/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class Person : Identifiable, IHasMeta

[HasMany("todo-items")]
public virtual List<TodoItem> TodoItems { get; set; }

[HasMany("assigned-todo-items")]
public virtual List<TodoItem> AssignedTodoItems { get; set; }

[HasMany("todo-item-collections")]
public virtual List<TodoItemCollection> TodoItemCollections { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions src/JsonApiDotNetCoreExample/Models/TodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ public class TodoItem : Identifiable
public long Ordinal { get; set; }

public int? OwnerId { get; set; }
public int? AssigneeId { get; set; }
public Guid? CollectionId { get; set; }

[HasOne("owner")]
public virtual Person Owner { get; set; }

[HasOne("assignee")]
public virtual Person Assignee { get; set; }

[HasOne("collection")]
public virtual TodoItemCollection Collection { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,40 @@ public async Task GET_Included_Contains_SideloadedData_OneToMany()
Assert.Equal(documents.Data.Count, documents.Included.Count);
}

[Fact]
public async Task GET_Included_DoesNot_Duplicate_Records_ForMultipleRelationshipsOfSameType()
{
// arrange
_context.People.RemoveRange(_context.People); // ensure all people have todo-items
_context.TodoItems.RemoveRange(_context.TodoItems);
var person = _personFaker.Generate();
var todoItem = _todoItemFaker.Generate();
todoItem.Owner = person;
todoItem.Assignee = person;
_context.TodoItems.Add(todoItem);
_context.SaveChanges();

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

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items/{todoItem.Id}?include=owner&include=assignee";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);
var documents = JsonConvert.DeserializeObject<Document>(await response.Content.ReadAsStringAsync());
var data = documents.Data;

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotEmpty(documents.Included);
Assert.Equal(1, documents.Included.Count);
}

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