Skip to content

Commit e577259

Browse files
committed
fix: added AttachHasManyThrough in defaultrepo (#425)
1 parent a49b83d commit e577259

29 files changed

+8732
-2
lines changed

src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace JsonApiDotNetCoreExample.Models
44
{
55
public class Tag : Identifiable
66
{
7+
[Attr("name")]
78
public string Name { get; set; }
89
}
910
}

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Reflection;
56
using System.Threading.Tasks;
67
using JsonApiDotNetCore.Extensions;
78
using JsonApiDotNetCore.Internal;
@@ -320,7 +321,9 @@ public virtual IQueryable<TEntity> Include(IQueryable<TEntity> entities, string
320321
entity = _jsonApiContext.ContextGraph.GetContextEntity(relationship.Type);
321322
}
322323

323-
return entities.Include(internalRelationshipPath);
324+
IQueryable<TEntity> included = entities.Include(internalRelationshipPath);
325+
AttachHasManyThrough(relationshipChain, included);
326+
return included;
324327
}
325328

326329
/// <inheritdoc />
@@ -367,5 +370,29 @@ public async Task<IReadOnlyList<TEntity>> ToListAsync(IQueryable<TEntity> entiti
367370
? await entities.ToListAsync()
368371
: entities.ToList();
369372
}
373+
374+
private void AttachHasManyThrough(string[] relationshipChain, IQueryable<TEntity> included)
375+
{
376+
var hasManyThroughRelationships = _jsonApiContext.RequestEntity.Relationships.OfType<HasManyThroughAttribute>();
377+
foreach (var hasManyThroughRelation in _jsonApiContext.RequestEntity.Relationships.OfType<HasManyThroughAttribute>())
378+
{
379+
if (relationshipChain.Contains(hasManyThroughRelation.PublicRelationshipName))
380+
{
381+
Type rightInstancesListType = typeof(List<>).MakeGenericType(hasManyThroughRelation.Type);
382+
PropertyInfo rightPropertyOnLeft = _jsonApiContext.RequestEntity.EntityType.GetProperty(hasManyThroughRelation.InternalRelationshipName);
383+
foreach (var leftInstance in included)
384+
{
385+
var rightInstancesList = (IList)Activator.CreateInstance(rightInstancesListType);
386+
var throughInstances = (IList)hasManyThroughRelation.ThroughProperty.GetValue(leftInstance);
387+
foreach (var ti in throughInstances)
388+
{
389+
var rightInstance = hasManyThroughRelation.RightProperty.GetValue(ti);
390+
rightInstancesList.Add(rightInstance);
391+
}
392+
rightPropertyOnLeft.SetValue(leftInstance, rightInstancesList);
393+
}
394+
}
395+
}
396+
}
370397
}
371398
}

src/JsonApiDotNetCore/JsonApiDotNetCore.csproj

100755100644
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
3030
<PackageReference Include="System.Memory" Version="4.5.0" />
3131
<PackageReference Include="System.ValueTuple" Version="$(TuplesVersion)" />
32-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-62925-02" PrivateAssets="All" />
3332
</ItemGroup>
3433

3534
<!--
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<!DOCTYPE html>
2+
<!--[if IE]><![endif]-->
3+
<html>
4+
5+
<head>
6+
<meta charset="utf-8">
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
8+
<title>PLACEHOLDER </title>
9+
<meta name="viewport" content="width=device-width">
10+
<meta name="title" content="PLACEHOLDER ">
11+
<meta name="generator" content="docfx 2.33.0.0">
12+
13+
<link rel="shortcut icon" href="../favicon.ico">
14+
<link rel="stylesheet" href="../styles/docfx.vendor.css">
15+
<link rel="stylesheet" href="../styles/docfx.css">
16+
<link rel="stylesheet" href="../styles/main.css">
17+
<meta property="docfx:navrel" content="../toc.html">
18+
<meta property="docfx:tocrel" content="../toc.html">
19+
20+
21+
22+
</head>
23+
<body data-spy="scroll" data-target="#affix">
24+
<div id="wrapper">
25+
<header>
26+
27+
<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
28+
<div class="container">
29+
<div class="navbar-header">
30+
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
31+
<span class="sr-only">Toggle navigation</span>
32+
<span class="icon-bar"></span>
33+
<span class="icon-bar"></span>
34+
<span class="icon-bar"></span>
35+
</button>
36+
37+
<a class="navbar-brand" href="../index.html">
38+
<img id="logo" class="svg" src="../logo.svg" alt="">
39+
</a>
40+
</div>
41+
<div class="collapse navbar-collapse" id="navbar">
42+
<form class="navbar-form navbar-right" role="search" id="search">
43+
<div class="form-group">
44+
<input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
45+
</div>
46+
</form>
47+
</div>
48+
</div>
49+
</nav>
50+
51+
<div class="subnav navbar navbar-default">
52+
<div class="container hide-when-search" id="breadcrumb">
53+
<ul class="breadcrumb">
54+
<li></li>
55+
</ul>
56+
</div>
57+
</div>
58+
</header>
59+
<div role="main" class="container body-content hide-when-search">
60+
<div class="article row grid">
61+
<div class="col-md-10">
62+
<article class="content wrap" id="_content" data-uid="">
63+
<h1 id="placeholder">PLACEHOLDER</h1>
64+
65+
<p>TODO: Add .NET projects to the <em>src</em> folder and run <code>docfx</code> to generate <strong>REAL</strong> <em>API Documentation</em>!</p>
66+
</article>
67+
</div>
68+
69+
<div class="hidden-sm col-md-2" role="complementary">
70+
<div class="sideaffix">
71+
<div class="contribution">
72+
<ul class="nav">
73+
<li>
74+
<a href="https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/fix/hasmanythrough-empty-includes/src/JsonApiDotNetCore/api/index.md/#L1" class="contribution-link">Improve this Doc</a>
75+
</li>
76+
</ul>
77+
</div>
78+
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
79+
<!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
80+
</nav>
81+
</div>
82+
</div>
83+
</div>
84+
</div>
85+
86+
<footer>
87+
<div class="grad-bottom"></div>
88+
<div class="footer">
89+
<div class="container">
90+
<span class="pull-right">
91+
<a href="#top">Back to top</a>
92+
</span>
93+
94+
<span>Generated by <strong>DocFX</strong></span>
95+
</div>
96+
</div>
97+
</footer>
98+
</div>
99+
100+
<script type="text/javascript" src="../styles/docfx.vendor.js"></script>
101+
<script type="text/javascript" src="../styles/docfx.js"></script>
102+
<script type="text/javascript" src="../styles/main.js"></script>
103+
</body>
104+
</html>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<!DOCTYPE html>
2+
<!--[if IE]><![endif]-->
3+
<html>
4+
5+
<head>
6+
<meta charset="utf-8">
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
8+
<title>Add your introductions here! </title>
9+
<meta name="viewport" content="width=device-width">
10+
<meta name="title" content="Add your introductions here! ">
11+
<meta name="generator" content="docfx 2.33.0.0">
12+
13+
<link rel="shortcut icon" href="../favicon.ico">
14+
<link rel="stylesheet" href="../styles/docfx.vendor.css">
15+
<link rel="stylesheet" href="../styles/docfx.css">
16+
<link rel="stylesheet" href="../styles/main.css">
17+
<meta property="docfx:navrel" content="../toc.html">
18+
<meta property="docfx:tocrel" content="toc.html">
19+
20+
21+
22+
</head>
23+
<body data-spy="scroll" data-target="#affix">
24+
<div id="wrapper">
25+
<header>
26+
27+
<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
28+
<div class="container">
29+
<div class="navbar-header">
30+
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
31+
<span class="sr-only">Toggle navigation</span>
32+
<span class="icon-bar"></span>
33+
<span class="icon-bar"></span>
34+
<span class="icon-bar"></span>
35+
</button>
36+
37+
<a class="navbar-brand" href="../index.html">
38+
<img id="logo" class="svg" src="../logo.svg" alt="">
39+
</a>
40+
</div>
41+
<div class="collapse navbar-collapse" id="navbar">
42+
<form class="navbar-form navbar-right" role="search" id="search">
43+
<div class="form-group">
44+
<input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
45+
</div>
46+
</form>
47+
</div>
48+
</div>
49+
</nav>
50+
51+
<div class="subnav navbar navbar-default">
52+
<div class="container hide-when-search" id="breadcrumb">
53+
<ul class="breadcrumb">
54+
<li></li>
55+
</ul>
56+
</div>
57+
</div>
58+
</header>
59+
<div role="main" class="container body-content hide-when-search">
60+
61+
<div class="sidenav hide-when-search">
62+
<a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
63+
<div class="sidetoggle collapse" id="sidetoggle">
64+
<div id="sidetoc"></div>
65+
</div>
66+
</div>
67+
<div class="article row grid-right">
68+
<div class="col-md-10">
69+
<article class="content wrap" id="_content" data-uid="">
70+
<h1 id="add-your-introductions-here">Add your introductions here!</h1>
71+
72+
</article>
73+
</div>
74+
75+
<div class="hidden-sm col-md-2" role="complementary">
76+
<div class="sideaffix">
77+
<div class="contribution">
78+
<ul class="nav">
79+
<li>
80+
<a href="https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/fix/hasmanythrough-empty-includes/src/JsonApiDotNetCore/articles/intro.md/#L1" class="contribution-link">Improve this Doc</a>
81+
</li>
82+
</ul>
83+
</div>
84+
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
85+
<!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
86+
</nav>
87+
</div>
88+
</div>
89+
</div>
90+
</div>
91+
92+
<footer>
93+
<div class="grad-bottom"></div>
94+
<div class="footer">
95+
<div class="container">
96+
<span class="pull-right">
97+
<a href="#top">Back to top</a>
98+
</span>
99+
100+
<span>Generated by <strong>DocFX</strong></span>
101+
</div>
102+
</div>
103+
</footer>
104+
</div>
105+
106+
<script type="text/javascript" src="../styles/docfx.vendor.js"></script>
107+
<script type="text/javascript" src="../styles/docfx.js"></script>
108+
<script type="text/javascript" src="../styles/main.js"></script>
109+
</body>
110+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
<div id="sidetoggle">
3+
<div>
4+
<div class="sidefilter">
5+
<form class="toc-filter">
6+
<span class="glyphicon glyphicon-filter filter-icon"></span>
7+
<input type="text" id="toc_filter_input" placeholder="Enter here to filter..." onkeypress="if(event.keyCode==13) {return false;}">
8+
</form>
9+
</div>
10+
<div class="sidetoc">
11+
<div class="toc" id="toc">
12+
13+
<ul class="nav level1">
14+
<li>
15+
<a href="intro.html" name="" title="Introduction">Introduction</a>
16+
</li>
17+
</ul> </div>
18+
</div>
19+
</div>
20+
</div>
97.3 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)