From 072ced3662dba5f0cb61ac306a3a1b6e15ed68b9 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Mon, 20 Mar 2017 15:02:45 -0500 Subject: [PATCH 1/3] test(fetching-data): add failing test checks whether or not included entities contain relationship data --- .../Acceptance/Spec/FetchingDataTests.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs index 075fc77129..7ff0b05e85 100644 --- a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingDataTests.cs @@ -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; @@ -13,6 +15,7 @@ using Microsoft.AspNetCore.TestHost; using Newtonsoft.Json; using Xunit; +using Person = JsonApiDotNetCoreExample.Models.Person; namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec { @@ -21,11 +24,19 @@ public class FetchingDataTests { private DocsFixture _fixture; private IJsonApiContext _jsonApiContext; + private Faker _todoItemFaker; + private Faker _personFaker; public FetchingDataTests(DocsFixture fixture) { _fixture = fixture; _jsonApiContext = fixture.GetService(); + _todoItemFaker = new Faker() + .RuleFor(t => t.Description, f => f.Lorem.Sentence()) + .RuleFor(t => t.Ordinal, f => f.Random.Number()); + _personFaker = new Faker() + .RuleFor(p => p.FirstName, f => f.Name.FirstName()) + .RuleFor(p => p.LastName, f => f.Name.LastName()); } [Fact] @@ -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(); + var todoItem = _todoItemFaker.Generate(); + var person = _personFaker.Generate(); + todoItem.Owner = person; + context.TodoItems.Add(todoItem); + await context.SaveChangesAsync(); + + var builder = new WebHostBuilder() + .UseStartup(); + 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(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(); + } } } From ec256649f5fe8d5e45d827bf686b9e69f2e55755 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Mon, 20 Mar 2017 15:03:50 -0500 Subject: [PATCH 2/3] feat(document-builder): include relationship objects in compound doc --- src/JsonApiDotNetCore/Builders/DocumentBuilder.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs index 63829021d7..7bc59a3b60 100644 --- a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs @@ -183,11 +183,7 @@ private DocumentData _getIncludedEntity(IIdentifiable entity) 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(); From 635e20f1f796f51f3bf44f85cb3523412814c038 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Mon, 20 Mar 2017 15:04:19 -0500 Subject: [PATCH 3/3] fix(document-builder): do not include null relationships in compound doc --- src/JsonApiDotNetCore/Builders/DocumentBuilder.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs index 7bc59a3b60..12710a4927 100644 --- a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs @@ -169,14 +169,21 @@ private List _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 entities, IIdentifiable entity) + { + var includedEntity = _getIncludedEntity(entity); + if(includedEntity != null) + entities.Add(includedEntity); + } + private DocumentData _getIncludedEntity(IIdentifiable entity) { if(entity == null) return null;