Skip to content

Fix/compound relationship inclusion #66

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 3 commits into from
Mar 20, 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
17 changes: 10 additions & 7 deletions src/JsonApiDotNetCore/Builders/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,28 @@ private List<DocumentData> _getIncludedEntities(ContextEntity contextEntity, IId

if (navigationEntity is IEnumerable)
foreach (var includedEntity in (IEnumerable)navigationEntity)
included.Add(_getIncludedEntity((IIdentifiable)includedEntity));
_addIncludedEntity(included, (IIdentifiable)includedEntity);
else
included.Add(_getIncludedEntity((IIdentifiable)navigationEntity));
_addIncludedEntity(included, (IIdentifiable)navigationEntity);
});

return included;
}

private void _addIncludedEntity(List<DocumentData> entities, IIdentifiable entity)
{
var includedEntity = _getIncludedEntity(entity);
if(includedEntity != null)
entities.Add(includedEntity);
}

private DocumentData _getIncludedEntity(IIdentifiable entity)
{
if(entity == null) return null;

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

var data = new DocumentData
{
Type = contextEntity.EntityName,
Id = entity.StringId
};
var data = _getData(contextEntity, entity);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Bogus;
using DotNetCoreDocs;
using DotNetCoreDocs.Writers;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample;
Expand All @@ -13,6 +15,7 @@
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json;
using Xunit;
using Person = JsonApiDotNetCoreExample.Models.Person;

namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
{
Expand All @@ -21,11 +24,19 @@ public class FetchingDataTests
{
private DocsFixture<Startup, JsonDocWriter> _fixture;
private IJsonApiContext _jsonApiContext;
private Faker<TodoItem> _todoItemFaker;
private Faker<Person> _personFaker;

public FetchingDataTests(DocsFixture<Startup, JsonDocWriter> fixture)
{
_fixture = fixture;
_jsonApiContext = fixture.GetService<IJsonApiContext>();
_todoItemFaker = new Faker<TodoItem>()
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
.RuleFor(t => t.Ordinal, f => f.Random.Number());
_personFaker = new Faker<Person>()
.RuleFor(p => p.FirstName, f => f.Name.FirstName())
.RuleFor(p => p.LastName, f => f.Name.LastName());
}

[Fact]
Expand Down Expand Up @@ -60,5 +71,38 @@ public async Task Request_ForEmptyCollection_Returns_EmptyDataCollection()

context.Dispose();
}

[Fact]
public async Task Included_Records_Contain_Relationship_Links()
{
// arrange
var context = _fixture.GetService<AppDbContext>();
var todoItem = _todoItemFaker.Generate();
var person = _personFaker.Generate();
todoItem.Owner = person;
context.TodoItems.Add(todoItem);
await context.SaveChangesAsync();

var builder = new WebHostBuilder()
.UseStartup<Startup>();
var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items/{todoItem.Id}?include=owner";
var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

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

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(person.StringId, deserializedBody.Included[0].Id);
Assert.NotNull(deserializedBody.Included[0].Relationships);
Assert.Equal($"http://localhost/api/v1/people/{person.Id}/todo-items", deserializedBody.Included[0].Relationships["todo-items"].Links.Related);
Assert.Equal($"http://localhost/api/v1/people/{person.Id}/relationships/todo-items", deserializedBody.Included[0].Relationships["todo-items"].Links.Self);
context.Dispose();
}
}
}