Skip to content

Commit abc7de0

Browse files
authoredFeb 21, 2017
Merge pull request #34 from Research-Institute/fix/#33
Fix/#33
2 parents ec1a1a7 + d8dfa47 commit abc7de0

File tree

3 files changed

+148
-5
lines changed

3 files changed

+148
-5
lines changed
 

‎src/JsonApiDotNetCore/Builders/LinkBuilder.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,26 @@ private string GetNamespaceFromPath(string path, string entityName)
2424
{
2525
var nSpace = string.Empty;
2626
var segments = path.Split('/');
27+
2728
for(var i = 1; i < segments.Length; i++)
2829
{
29-
if(segments[i].ToLower() == entityName.ToLower())
30+
if(segments[i].ToLower() == entityName.Dasherize())
3031
break;
31-
32+
3233
nSpace += $"/{segments[i].Dasherize()}";
3334
}
35+
3436
return nSpace;
3537
}
3638

3739
public string GetSelfRelationLink(string parent, string parentId, string child)
3840
{
39-
return $"{_context.BasePath}/relationships/{child.Dasherize()}";
41+
return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/relationships/{child.Dasherize()}";
4042
}
4143

4244
public string GetRelatedRelationLink(string parent, string parentId, string child)
4345
{
44-
return $"{_context.BasePath}/{child.Dasherize()}";
46+
return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/{child.Dasherize()}";
4547
}
4648
}
4749
}

‎src/JsonApiDotNetCore/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.2.9",
2+
"version": "0.2.10",
33

44
"dependencies": {
55
"Microsoft.NETCore.App": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System.Net;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using DotNetCoreDocs;
5+
using DotNetCoreDocs.Writers;
6+
using JsonApiDotNetCoreExample;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.AspNetCore.TestHost;
9+
using Newtonsoft.Json;
10+
using Xunit;
11+
using JsonApiDotNetCore.Models;
12+
using JsonApiDotNetCoreExample.Data;
13+
using System.Linq;
14+
using System;
15+
16+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
17+
{
18+
[Collection("WebHostCollection")]
19+
public class Relationships
20+
{
21+
private DocsFixture<Startup, JsonDocWriter> _fixture;
22+
private AppDbContext _context;
23+
public Relationships(DocsFixture<Startup, JsonDocWriter> fixture)
24+
{
25+
_fixture = fixture;
26+
_context = fixture.GetService<AppDbContext>();
27+
}
28+
29+
[Fact]
30+
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
31+
{
32+
// arrange
33+
var builder = new WebHostBuilder()
34+
.UseStartup<Startup>();
35+
36+
var httpMethod = new HttpMethod("GET");
37+
var route = $"/api/v1/todo-items";
38+
39+
var server = new TestServer(builder);
40+
var client = server.CreateClient();
41+
var request = new HttpRequestMessage(httpMethod, route);
42+
43+
// act
44+
var response = await client.SendAsync(request);
45+
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
46+
var data = documents.Data[0];
47+
var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{data.Id}/relationships/owner";
48+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{data.Id}/owner";
49+
50+
// assert
51+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
52+
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links.Self);
53+
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related);
54+
}
55+
56+
[Fact]
57+
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById()
58+
{
59+
// arrange
60+
var todoItemId = _context.TodoItems.Last().Id;
61+
62+
var builder = new WebHostBuilder()
63+
.UseStartup<Startup>();
64+
65+
var httpMethod = new HttpMethod("GET");
66+
var route = $"/api/v1/todo-items/{todoItemId}";
67+
68+
var server = new TestServer(builder);
69+
var client = server.CreateClient();
70+
var request = new HttpRequestMessage(httpMethod, route);
71+
72+
// act
73+
var response = await client.SendAsync(request);
74+
var responseString = await response.Content.ReadAsStringAsync();
75+
var data = JsonConvert.DeserializeObject<Document>(responseString).Data;
76+
var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{todoItemId}/relationships/owner";
77+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{todoItemId}/owner";
78+
79+
// assert
80+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
81+
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links?.Self);
82+
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related);
83+
}
84+
85+
[Fact]
86+
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships()
87+
{
88+
// arrange
89+
var builder = new WebHostBuilder()
90+
.UseStartup<Startup>();
91+
92+
var httpMethod = new HttpMethod("GET");
93+
var route = $"/api/v1/people";
94+
95+
var server = new TestServer(builder);
96+
var client = server.CreateClient();
97+
var request = new HttpRequestMessage(httpMethod, route);
98+
99+
// act
100+
var response = await client.SendAsync(request);
101+
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
102+
var data = documents.Data[0];
103+
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{data.Id}/relationships/todo-items";
104+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{data.Id}/todo-items";
105+
106+
// assert
107+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
108+
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links.Self);
109+
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related);
110+
}
111+
112+
[Fact]
113+
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById()
114+
{
115+
// arrange
116+
var personId = _context.People.Last().Id;
117+
118+
var builder = new WebHostBuilder()
119+
.UseStartup<Startup>();
120+
121+
var httpMethod = new HttpMethod("GET");
122+
var route = $"/api/v1/people/{personId}";
123+
124+
var server = new TestServer(builder);
125+
var client = server.CreateClient();
126+
var request = new HttpRequestMessage(httpMethod, route);
127+
128+
// act
129+
var response = await client.SendAsync(request);
130+
var responseString = await response.Content.ReadAsStringAsync();
131+
var data = JsonConvert.DeserializeObject<Document>(responseString).Data;
132+
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{personId}/relationships/todo-items";
133+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{personId}/todo-items";
134+
135+
// assert
136+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
137+
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links?.Self);
138+
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related);
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)
Please sign in to comment.