Skip to content

Commit c45925b

Browse files
committed
fix #453: support 'null' for HasManyThrough relationships when building resource graph
1 parent 64a18e6 commit c45925b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/JsonApiDotNetCore/Internal/ResourceGraph.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,18 @@ private IEnumerable<IIdentifiable> GetHasManyThrough(IIdentifiable parent, HasMa
144144
var throughProperty = GetRelationship(parent, hasManyThrough.InternalThroughName);
145145
if (throughProperty is IEnumerable hasManyNavigationEntity)
146146
{
147-
foreach (var includedEntity in hasManyNavigationEntity)
148-
{
149-
var targetValue = hasManyThrough.RightProperty.GetValue(includedEntity) as IIdentifiable;
150-
yield return targetValue;
151-
}
147+
// wrap "yield return" in a sub-function so we can correctly return null if the property is null.
148+
return GetHasManyThroughIter(hasManyThrough, hasManyNavigationEntity);
149+
}
150+
return null;
151+
}
152+
153+
private IEnumerable<IIdentifiable> GetHasManyThroughIter(HasManyThroughAttribute hasManyThrough, IEnumerable hasManyNavigationEntity)
154+
{
155+
foreach (var includedEntity in hasManyNavigationEntity)
156+
{
157+
var targetValue = hasManyThrough.RightProperty.GetValue(includedEntity) as IIdentifiable;
158+
yield return targetValue;
152159
}
153160
}
154161

0 commit comments

Comments
 (0)