From 7f84dd2dc1efe9d309f069d7a46d863e487d1c06 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 21 Feb 2017 17:02:22 -0600 Subject: [PATCH 1/5] test(relationships): add failing tests --- .../Acceptance/Spec/Relationships.cs | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs new file mode 100644 index 0000000000..c87cbeed0c --- /dev/null +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs @@ -0,0 +1,80 @@ +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using DotNetCoreDocs; +using DotNetCoreDocs.Models; +using DotNetCoreDocs.Writers; +using JsonApiDotNetCoreExample; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Newtonsoft.Json; +using Xunit; +using JsonApiDotNetCore.Internal; +using JsonApiDotNetCore.Models; + +namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec +{ + [Collection("WebHostCollection")] + public class Relationships + { + private DocsFixture _fixture; + public Relationships(DocsFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships() + { + // arrange + var builder = new WebHostBuilder() + .UseStartup(); + + var httpMethod = new HttpMethod("GET"); + var route = $"/api/v1/todo-items"; + + 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(await response.Content.ReadAsStringAsync()); + var data = documents.Data[0]; + var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{data.Id}/relationships/owner"; + var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{data.Id}/owner"; + + // assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links.Self); + Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related); + } + + [Fact] + public async Task Correct_RelationshipObjects_For_OneToMany_Relationships() + { + // arrange + var builder = new WebHostBuilder() + .UseStartup(); + + var httpMethod = new HttpMethod("GET"); + var route = $"/api/v1/people"; + + 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(await response.Content.ReadAsStringAsync()); + var data = documents.Data[0]; + var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{data.Id}/relationships/todo-items"; + var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{data.Id}/todo-items"; + + // assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links.Self); + Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related); + } + } +} From a741d371d3afe195d712b8239b6829160c8ad1ed Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 21 Feb 2017 17:05:08 -0600 Subject: [PATCH 2/5] fix(link-builder): include entity name in the base path --- src/JsonApiDotNetCore/Builders/DocumentBuilder.cs | 4 ++-- src/JsonApiDotNetCore/Builders/LinkBuilder.cs | 15 ++++++--------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs index bd06f9936d..16201d094d 100644 --- a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs @@ -83,8 +83,8 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I { Links = new Links { - Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName), - Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName) + Self = linkBuilder.GetSelfRelationLink(entity.Id.ToString(), r.RelationshipName), + Related = linkBuilder.GetRelatedRelationLink(entity.Id.ToString(), r.RelationshipName) } }; diff --git a/src/JsonApiDotNetCore/Builders/LinkBuilder.cs b/src/JsonApiDotNetCore/Builders/LinkBuilder.cs index a6c2c9cf37..3f72e4b588 100644 --- a/src/JsonApiDotNetCore/Builders/LinkBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/LinkBuilder.cs @@ -24,24 +24,21 @@ private string GetNamespaceFromPath(string path, string entityName) { var nSpace = string.Empty; var segments = path.Split('/'); + for(var i = 1; i < segments.Length; i++) - { - if(segments[i].ToLower() == entityName.ToLower()) - break; - nSpace += $"/{segments[i].Dasherize()}"; - } + return nSpace; } - public string GetSelfRelationLink(string parent, string parentId, string child) + public string GetSelfRelationLink(string parentId, string child) { - return $"{_context.BasePath}/relationships/{child.Dasherize()}"; + return $"{_context.BasePath}/{parentId}/relationships/{child.Dasherize()}"; } - public string GetRelatedRelationLink(string parent, string parentId, string child) + public string GetRelatedRelationLink(string parentId, string child) { - return $"{_context.BasePath}/{child.Dasherize()}"; + return $"{_context.BasePath}/{parentId}/{child.Dasherize()}"; } } } From 494191c7b520e4b957c3b2d355479b4236e36767 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 21 Feb 2017 17:24:21 -0600 Subject: [PATCH 3/5] test(relationships): add failing tests --- .../Acceptance/Spec/Relationships.cs | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs index c87cbeed0c..fd11cb47b5 100644 --- a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs @@ -2,15 +2,16 @@ using System.Net.Http; using System.Threading.Tasks; using DotNetCoreDocs; -using DotNetCoreDocs.Models; using DotNetCoreDocs.Writers; using JsonApiDotNetCoreExample; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; using Newtonsoft.Json; using Xunit; -using JsonApiDotNetCore.Internal; using JsonApiDotNetCore.Models; +using JsonApiDotNetCoreExample.Data; +using System.Linq; +using System; namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec { @@ -18,9 +19,11 @@ namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec public class Relationships { private DocsFixture _fixture; + private AppDbContext _context; public Relationships(DocsFixture fixture) { _fixture = fixture; + _context = fixture.GetService(); } [Fact] @@ -50,6 +53,35 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships() Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related); } + [Fact] + public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById() + { + // arrange + var todoItemId = _context.TodoItems.Last().Id; + + var builder = new WebHostBuilder() + .UseStartup(); + + var httpMethod = new HttpMethod("GET"); + var route = $"/api/v1/todo-items/{todoItemId}"; + + var server = new TestServer(builder); + var client = server.CreateClient(); + var request = new HttpRequestMessage(httpMethod, route); + + // act + var response = await client.SendAsync(request); + var responseString = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(responseString).Data; + var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{todoItemId}/relationships/owner"; + var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{todoItemId}/owner"; + + // assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links?.Self); + Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related); + } + [Fact] public async Task Correct_RelationshipObjects_For_OneToMany_Relationships() { @@ -76,5 +108,34 @@ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships() Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links.Self); Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related); } + + [Fact] + public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById() + { + // arrange + var personId = _context.People.Last().Id; + + var builder = new WebHostBuilder() + .UseStartup(); + + var httpMethod = new HttpMethod("GET"); + var route = $"/api/v1/people/{personId}"; + + var server = new TestServer(builder); + var client = server.CreateClient(); + var request = new HttpRequestMessage(httpMethod, route); + + // act + var response = await client.SendAsync(request); + var responseString = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(responseString).Data; + var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{personId}/relationships/todo-items"; + var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{personId}/todo-items"; + + // assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links?.Self); + Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related); + } } } From d7a6556420fdd42da59ecfbff4dcae893006f5f1 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 21 Feb 2017 17:26:05 -0600 Subject: [PATCH 4/5] fix(link-builder): do not include entity name in base path the entity name is used to determine the end of the base path for example /api/v1/my-entities/1 the base path should just be /api/v1 --- src/JsonApiDotNetCore/Builders/DocumentBuilder.cs | 4 ++-- src/JsonApiDotNetCore/Builders/LinkBuilder.cs | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs index 16201d094d..bd06f9936d 100644 --- a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs @@ -83,8 +83,8 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I { Links = new Links { - Self = linkBuilder.GetSelfRelationLink(entity.Id.ToString(), r.RelationshipName), - Related = linkBuilder.GetRelatedRelationLink(entity.Id.ToString(), r.RelationshipName) + Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName), + Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName) } }; diff --git a/src/JsonApiDotNetCore/Builders/LinkBuilder.cs b/src/JsonApiDotNetCore/Builders/LinkBuilder.cs index 3f72e4b588..d819aab253 100644 --- a/src/JsonApiDotNetCore/Builders/LinkBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/LinkBuilder.cs @@ -26,19 +26,24 @@ private string GetNamespaceFromPath(string path, string entityName) var segments = path.Split('/'); for(var i = 1; i < segments.Length; i++) - nSpace += $"/{segments[i].Dasherize()}"; + { + if(segments[i].ToLower() == entityName.Dasherize()) + break; + nSpace += $"/{segments[i].Dasherize()}"; + } + return nSpace; } - public string GetSelfRelationLink(string parentId, string child) + public string GetSelfRelationLink(string parent, string parentId, string child) { - return $"{_context.BasePath}/{parentId}/relationships/{child.Dasherize()}"; + return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/relationships/{child.Dasherize()}"; } - public string GetRelatedRelationLink(string parentId, string child) + public string GetRelatedRelationLink(string parent, string parentId, string child) { - return $"{_context.BasePath}/{parentId}/{child.Dasherize()}"; + return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/{child.Dasherize()}"; } } } From d8dfa472ffd367c10657cdb18bdc05b69b3eb1d1 Mon Sep 17 00:00:00 2001 From: Jared Nance Date: Tue, 21 Feb 2017 17:27:11 -0600 Subject: [PATCH 5/5] chore(project.json): bump package version --- src/JsonApiDotNetCore/project.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonApiDotNetCore/project.json b/src/JsonApiDotNetCore/project.json index 8e362d4459..1a028b08ae 100644 --- a/src/JsonApiDotNetCore/project.json +++ b/src/JsonApiDotNetCore/project.json @@ -1,5 +1,5 @@ { - "version": "0.2.9", + "version": "0.2.10", "dependencies": { "Microsoft.NETCore.App": {