Skip to content

Commit e2c0cc8

Browse files
authored
Merge pull request #66 from Research-Institute/fix/compound-rel-inclusion
Fix/compound relationship inclusion
2 parents b322162 + 635e20f commit e2c0cc8

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,28 @@ private List<DocumentData> _getIncludedEntities(ContextEntity contextEntity, IId
169169

170170
if (navigationEntity is IEnumerable)
171171
foreach (var includedEntity in (IEnumerable)navigationEntity)
172-
included.Add(_getIncludedEntity((IIdentifiable)includedEntity));
172+
_addIncludedEntity(included, (IIdentifiable)includedEntity);
173173
else
174-
included.Add(_getIncludedEntity((IIdentifiable)navigationEntity));
174+
_addIncludedEntity(included, (IIdentifiable)navigationEntity);
175175
});
176176

177177
return included;
178178
}
179179

180+
private void _addIncludedEntity(List<DocumentData> entities, IIdentifiable entity)
181+
{
182+
var includedEntity = _getIncludedEntity(entity);
183+
if(includedEntity != null)
184+
entities.Add(includedEntity);
185+
}
186+
180187
private DocumentData _getIncludedEntity(IIdentifiable entity)
181188
{
182189
if(entity == null) return null;
183190

184191
var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(entity.GetType());
185192

186-
var data = new DocumentData
187-
{
188-
Type = contextEntity.EntityName,
189-
Id = entity.StringId
190-
};
193+
var data = _getData(contextEntity, entity);
191194

192195
data.Attributes = new Dictionary<string, object>();
193196

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs

+44
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
using System.Net;
33
using System.Net.Http;
44
using System.Threading.Tasks;
5+
using Bogus;
56
using DotNetCoreDocs;
67
using DotNetCoreDocs.Writers;
8+
using JsonApiDotNetCore.Models;
79
using JsonApiDotNetCore.Serialization;
810
using JsonApiDotNetCore.Services;
911
using JsonApiDotNetCoreExample;
@@ -13,6 +15,7 @@
1315
using Microsoft.AspNetCore.TestHost;
1416
using Newtonsoft.Json;
1517
using Xunit;
18+
using Person = JsonApiDotNetCoreExample.Models.Person;
1619

1720
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
1821
{
@@ -21,11 +24,19 @@ public class FetchingDataTests
2124
{
2225
private DocsFixture<Startup, JsonDocWriter> _fixture;
2326
private IJsonApiContext _jsonApiContext;
27+
private Faker<TodoItem> _todoItemFaker;
28+
private Faker<Person> _personFaker;
2429

2530
public FetchingDataTests(DocsFixture<Startup, JsonDocWriter> fixture)
2631
{
2732
_fixture = fixture;
2833
_jsonApiContext = fixture.GetService<IJsonApiContext>();
34+
_todoItemFaker = new Faker<TodoItem>()
35+
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
36+
.RuleFor(t => t.Ordinal, f => f.Random.Number());
37+
_personFaker = new Faker<Person>()
38+
.RuleFor(p => p.FirstName, f => f.Name.FirstName())
39+
.RuleFor(p => p.LastName, f => f.Name.LastName());
2940
}
3041

3142
[Fact]
@@ -60,5 +71,38 @@ public async Task Request_ForEmptyCollection_Returns_EmptyDataCollection()
6071

6172
context.Dispose();
6273
}
74+
75+
[Fact]
76+
public async Task Included_Records_Contain_Relationship_Links()
77+
{
78+
// arrange
79+
var context = _fixture.GetService<AppDbContext>();
80+
var todoItem = _todoItemFaker.Generate();
81+
var person = _personFaker.Generate();
82+
todoItem.Owner = person;
83+
context.TodoItems.Add(todoItem);
84+
await context.SaveChangesAsync();
85+
86+
var builder = new WebHostBuilder()
87+
.UseStartup<Startup>();
88+
var httpMethod = new HttpMethod("GET");
89+
var route = $"/api/v1/todo-items/{todoItem.Id}?include=owner";
90+
var server = new TestServer(builder);
91+
var client = server.CreateClient();
92+
var request = new HttpRequestMessage(httpMethod, route);
93+
94+
// act
95+
var response = await client.SendAsync(request);
96+
var body = await response.Content.ReadAsStringAsync();
97+
var deserializedBody = JsonConvert.DeserializeObject<Document>(body);
98+
99+
// assert
100+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
101+
Assert.Equal(person.StringId, deserializedBody.Included[0].Id);
102+
Assert.NotNull(deserializedBody.Included[0].Relationships);
103+
Assert.Equal($"http://localhost/api/v1/people/{person.Id}/todo-items", deserializedBody.Included[0].Relationships["todo-items"].Links.Related);
104+
Assert.Equal($"http://localhost/api/v1/people/{person.Id}/relationships/todo-items", deserializedBody.Included[0].Relationships["todo-items"].Links.Self);
105+
context.Dispose();
106+
}
63107
}
64108
}

0 commit comments

Comments
 (0)