Skip to content

Commit ad85758

Browse files
committed
fix: traversal bug related to parsing next layer relationships when layer count > 2
1 parent d6d4a61 commit ad85758

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,5 +430,13 @@ public static IQueryable<T> PageForward<T>(this IQueryable<T> source, int pageSi
430430
return source;
431431
}
432432

433+
public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
434+
{
435+
foreach (T item in enumeration)
436+
{
437+
action(item);
438+
}
439+
}
440+
433441
}
434442
}

src/JsonApiDotNetCore/Hooks/Traversal/TraversalHelper.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public TraversalHelper(
5555
public RootNode<TEntity> CreateRootNode<TEntity>(IEnumerable<TEntity> rootEntities) where TEntity : class, IIdentifiable
5656
{
5757
_processedEntities = new Dictionary<DependentType, HashSet<IIdentifiable>>();
58+
RegisterRelationshipProxies(typeof(TEntity));
5859
var uniqueEntities = ProcessEntities(rootEntities);
5960
var relationshipsToNextLayer = GetRelationships(typeof(TEntity));
6061
return new RootNode<TEntity>(uniqueEntities, relationshipsToNextLayer);
@@ -94,7 +95,7 @@ public EntityChildLayer CreateNextLayer(IEnumerable<IEntityNode> nodes)
9495
return CreateRelationsipGroupInstance(nextNodeType, proxy, grouped.Value, principals[proxy]);
9596
}).ToList();
9697

97-
98+
RegisterRelationshipProxies(nextNodeType);
9899
return CreateNodeInstance(nextNodeType, GetRelationships(nextNodeType), relationshipsToPreviousLayer);
99100
}).ToList();
100101

@@ -118,6 +119,8 @@ Dictionary<DependentType, List<KeyValuePair<RelationshipProxy, List<IIdentifiabl
118119
var principalsGrouped = new Dictionary<RelationshipProxy, List<IIdentifiable>>();
119120
var dependentsGrouped = new Dictionary<RelationshipProxy, List<IIdentifiable>>();
120121

122+
principalNodes.ForEach(n => RegisterRelationshipProxies(n.EntityType));
123+
121124
foreach (var node in principalNodes)
122125
{
123126
var principalEntities = node.UniqueEntities;
@@ -181,7 +184,11 @@ HashSet<TEntity> ProcessEntities<TEntity>(IEnumerable<TEntity> incomingEntities)
181184
Type type = typeof(TEntity);
182185
var newEntities = UniqueInTree(incomingEntities, type);
183186
RegisterProcessedEntities(newEntities, type);
187+
return newEntities;
188+
}
184189

190+
void RegisterRelationshipProxies(DependentType type)
191+
{
185192
var contextEntity = _graph.GetContextEntity(type);
186193
foreach (RelationshipAttribute attr in contextEntity.Relationships)
187194
{
@@ -194,7 +201,6 @@ HashSet<TEntity> ProcessEntities<TEntity>(IEnumerable<TEntity> incomingEntities)
194201
RelationshipProxies[attr] = proxy;
195202
}
196203
}
197-
return newEntities;
198204
}
199205

200206

src/JsonApiDotNetCore/Models/RelationshipAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ public override bool Equals(object obj)
9999
{
100100
return false;
101101
}
102-
return IsHasMany == attr.IsHasMany && PublicRelationshipName.Equals(attr.PublicRelationshipName);
102+
bool equalRelationshipName = PublicRelationshipName.Equals(attr.PublicRelationshipName);
103+
bool equalPrincipalType = PrincipalType.Equals(attr.PrincipalType);
104+
return IsHasMany == attr.IsHasMany && equalRelationshipName && equalPrincipalType;
103105
}
104106

105107
/// <summary>

0 commit comments

Comments
 (0)