diff --git a/src/JsonApiDotNetCore/Builders/LinkBuilder.cs b/src/JsonApiDotNetCore/Builders/LinkBuilder.cs index a6c2c9cf37..d819aab253 100644 --- a/src/JsonApiDotNetCore/Builders/LinkBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/LinkBuilder.cs @@ -24,24 +24,26 @@ 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()) + if(segments[i].ToLower() == entityName.Dasherize()) break; - + nSpace += $"/{segments[i].Dasherize()}"; } + return nSpace; } public string GetSelfRelationLink(string parent, string parentId, string child) { - return $"{_context.BasePath}/relationships/{child.Dasherize()}"; + return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/relationships/{child.Dasherize()}"; } public string GetRelatedRelationLink(string parent, string parentId, string child) { - return $"{_context.BasePath}/{child.Dasherize()}"; + return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/{child.Dasherize()}"; } } } 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": { diff --git a/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs new file mode 100644 index 0000000000..fd11cb47b5 --- /dev/null +++ b/test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs @@ -0,0 +1,141 @@ +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using DotNetCoreDocs; +using DotNetCoreDocs.Writers; +using JsonApiDotNetCoreExample; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Newtonsoft.Json; +using Xunit; +using JsonApiDotNetCore.Models; +using JsonApiDotNetCoreExample.Data; +using System.Linq; +using System; + +namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec +{ + [Collection("WebHostCollection")] + public class Relationships + { + private DocsFixture _fixture; + private AppDbContext _context; + public Relationships(DocsFixture fixture) + { + _fixture = fixture; + _context = fixture.GetService(); + } + + [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_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() + { + // 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); + } + + [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); + } + } +}