Skip to content

Commit cbc37d1

Browse files
authored
Merge pull request #427 from maurei/fix/hasmanythrough-empty-includes
fix(#425): fetching HasManyThrough returns no includes
2 parents 7efb3c5 + 4dcf7bf commit cbc37d1

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

Diff for: src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace JsonApiDotNetCoreExample.Models
44
{
55
public class Tag : Identifiable
66
{
7+
[Attr]
78
public string Name { get; set; }
89
}
910
}

Diff for: src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private List<ResourceObject> IncludeRelationshipChain(
217217
if(relationship == null)
218218
throw new JsonApiException(400, $"{parentEntity.EntityName} does not contain relationship {requestedRelationship}");
219219

220-
var navigationEntity = _jsonApiContext.ContextGraph.GetRelationship(parentResource, relationship.InternalRelationshipName);
220+
var navigationEntity = _jsonApiContext.ContextGraph.GetRelationshipValue(parentResource, relationship);
221221
if (navigationEntity is IEnumerable hasManyNavigationEntity)
222222
{
223223
foreach (IIdentifiable includedEntity in hasManyNavigationEntity)

Diff for: src/JsonApiDotNetCore/JsonApiDotNetCore.csproj

100755100644
File mode changed.

Diff for: test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs

+54-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Net;
45
using System.Net.Http;
56
using System.Net.Http.Headers;
67
using System.Threading.Tasks;
78
using Bogus;
9+
using JsonApiDotNetCore.Models;
810
using JsonApiDotNetCore.Serialization;
911
using JsonApiDotNetCoreExample.Data;
1012
using JsonApiDotNetCoreExample.Models;
@@ -24,25 +26,69 @@ public class ManyToManyTests
2426
private static readonly Faker<Tag> _tagFaker = new Faker<Tag>().RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));
2527

2628
private TestFixture<TestStartup> _fixture;
27-
public ManyToManyTests(TestFixture<TestStartup> fixture)
29+
public ManyToManyTests(TestFixture<TestStartup> fixture)
2830
{
2931
_fixture = fixture;
3032
}
3133

3234
[Fact]
33-
public async Task Can_Fetch_Many_To_Many_Through()
35+
public async Task Can_Fetch_Many_To_Many_Through_All()
3436
{
3537
// arrange
3638
var context = _fixture.GetService<AppDbContext>();
3739
var article = _articleFaker.Generate();
3840
var tag = _tagFaker.Generate();
39-
var articleTag = new ArticleTag {
41+
42+
context.Articles.RemoveRange(context.Articles);
43+
await context.SaveChangesAsync();
44+
45+
var articleTag = new ArticleTag
46+
{
4047
Article = article,
4148
Tag = tag
4249
};
4350
context.ArticleTags.Add(articleTag);
4451
await context.SaveChangesAsync();
52+
53+
var route = $"/api/v1/articles?include=tags";
54+
55+
// act
56+
var response = await _fixture.Client.GetAsync(route);
57+
58+
// assert
59+
var body = await response.Content.ReadAsStringAsync();
60+
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");
61+
62+
var document = JsonConvert.DeserializeObject<Documents>(body);
63+
Assert.NotEmpty(document.Included);
64+
65+
var articleResponseList = _fixture.GetService<IJsonApiDeSerializer>().DeserializeList<Article>(body);
66+
Assert.NotNull(articleResponseList);
4567

68+
var articleResponse = articleResponseList.FirstOrDefault(a => a.Id == article.Id);
69+
Assert.NotNull(articleResponse);
70+
Assert.Equal(article.Name, articleResponse.Name);
71+
72+
var tagResponse = Assert.Single(articleResponse.Tags);
73+
Assert.Equal(tag.Id, tagResponse.Id);
74+
Assert.Equal(tag.Name, tagResponse.Name);
75+
}
76+
77+
[Fact]
78+
public async Task Can_Fetch_Many_To_Many_Through_GetById()
79+
{
80+
// arrange
81+
var context = _fixture.GetService<AppDbContext>();
82+
var article = _articleFaker.Generate();
83+
var tag = _tagFaker.Generate();
84+
var articleTag = new ArticleTag
85+
{
86+
Article = article,
87+
Tag = tag
88+
};
89+
context.ArticleTags.Add(articleTag);
90+
await context.SaveChangesAsync();
91+
4692
var route = $"/api/v1/articles/{article.Id}?include=tags";
4793

4894
// act
@@ -52,12 +98,16 @@ public async Task Can_Fetch_Many_To_Many_Through()
5298
var body = await response.Content.ReadAsStringAsync();
5399
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");
54100

101+
var document = JsonConvert.DeserializeObject<Document>(body);
102+
Assert.NotEmpty(document.Included);
103+
55104
var articleResponse = _fixture.GetService<IJsonApiDeSerializer>().Deserialize<Article>(body);
56105
Assert.NotNull(articleResponse);
57106
Assert.Equal(article.Id, articleResponse.Id);
58-
107+
59108
var tagResponse = Assert.Single(articleResponse.Tags);
60109
Assert.Equal(tag.Id, tagResponse.Id);
110+
Assert.Equal(tag.Name, tagResponse.Name);
61111
}
62112

63113
[Fact]

0 commit comments

Comments
 (0)