Skip to content

Commit 87747c0

Browse files
committed
test(patch): can update to-one relationships
Issue #21
1 parent 43bd9e6 commit 87747c0

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/JsonApiDotNetCore/Internal/Generics/GenericProcessor.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System.Collections;
12
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Reflection;
35
using System.Threading.Tasks;
46
using JsonApiDotNetCore.Extensions;
57
using JsonApiDotNetCore.Models;
@@ -19,8 +21,17 @@ public async Task UpdateRelationshipsAsync(object parent, Relationship relations
1921
{
2022
var relationshipType = relationship.BaseType;
2123

22-
var entities = _context.GetDbSet<T>().Where(x => relationshipIds.Contains(x.Id.ToString())).ToList();
23-
relationship.SetValue(parent, entities);
24+
// TODO: replace with relationship.IsMany
25+
if(relationship.Type.GetInterfaces().Contains(typeof(IEnumerable)))
26+
{
27+
var entities = _context.GetDbSet<T>().Where(x => relationshipIds.Contains(x.Id.ToString())).ToList();
28+
relationship.SetValue(parent, entities);
29+
}
30+
else
31+
{
32+
var entity = _context.GetDbSet<T>().SingleOrDefault(x => relationshipIds.First() == x.Id.ToString());
33+
relationship.SetValue(parent, entity);
34+
}
2435

2536
await _context.SaveChangesAsync();
2637
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingRelationshipsTests.cs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public UpdatingRelationshipsTests(DocsFixture<Startup, JsonDocWriter> fixture)
4141
}
4242

4343
[Fact]
44-
public async Task Can_Update_ToManyRelationship_ThroughLink()
44+
public async Task Can_Update_ToMany_Relationship_ThroughLink()
4545
{
4646
// arrange
4747
var person = _personFaker.Generate();
@@ -57,7 +57,7 @@ public async Task Can_Update_ToManyRelationship_ThroughLink()
5757

5858
var server = new TestServer(builder);
5959
var client = server.CreateClient();
60-
60+
6161
var content = new
6262
{
6363
data = new List<object>
@@ -84,5 +84,48 @@ public async Task Can_Update_ToManyRelationship_ThroughLink()
8484
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
8585
Assert.NotEmpty(personsTodoItems);
8686
}
87+
88+
[Fact]
89+
public async Task Can_Update_ToOne_Relationship_ThroughLink()
90+
{
91+
// arrange
92+
var person = _personFaker.Generate();
93+
_context.People.Add(person);
94+
95+
var todoItem = _todoItemFaker.Generate();
96+
_context.TodoItems.Add(todoItem);
97+
98+
_context.SaveChanges();
99+
100+
var builder = new WebHostBuilder()
101+
.UseStartup<Startup>();
102+
103+
var server = new TestServer(builder);
104+
var client = server.CreateClient();
105+
106+
var content = new
107+
{
108+
data = new
109+
{
110+
type = "person",
111+
id = $"{person.Id}"
112+
}
113+
};
114+
115+
var httpMethod = new HttpMethod("PATCH");
116+
var route = $"/api/v1/todo-items/{todoItem.Id}/relationships/owner";
117+
var request = new HttpRequestMessage(httpMethod, route);
118+
119+
request.Content = new StringContent(JsonConvert.SerializeObject(content));
120+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
121+
122+
// Act
123+
var response = await client.SendAsync(request);
124+
var todoItemsOwner = _context.TodoItems.Include(t => t.Owner).Single(t => t.Id == todoItem.Id);
125+
126+
// Assert
127+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
128+
Assert.NotNull(todoItemsOwner);
129+
}
87130
}
88131
}

0 commit comments

Comments
 (0)