@@ -192,24 +192,62 @@ private RelationshipData GetRelationshipData(RelationshipAttribute attr, Context
192192 return relationshipData ;
193193 }
194194
195- private List < DocumentData > GetIncludedEntities ( List < DocumentData > included , ContextEntity contextEntity , IIdentifiable entity )
195+ private List < DocumentData > GetIncludedEntities ( List < DocumentData > included , ContextEntity rootContextEntity , IIdentifiable rootResource )
196196 {
197- contextEntity . Relationships . ForEach ( r =>
197+ if ( _jsonApiContext . IncludedRelationships != null )
198198 {
199- if ( ! RelationshipIsIncluded ( r . PublicRelationshipName ) ) return ;
199+ foreach ( var relationshipName in _jsonApiContext . IncludedRelationships )
200+ {
201+ var relationshipChain = relationshipName . Split ( '.' ) ;
200202
201- var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( entity , r . InternalRelationshipName ) ;
203+ var contextEntity = rootContextEntity ;
204+ var entity = rootResource ;
205+ included = IncludeRelationshipChain ( included , rootContextEntity , rootResource , relationshipChain , 0 ) ;
206+ }
207+ }
202208
203- if ( navigationEntity is IEnumerable hasManyNavigationEntity )
204- foreach ( IIdentifiable includedEntity in hasManyNavigationEntity )
205- included = AddIncludedEntity ( included , includedEntity ) ;
206- else
207- included = AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
208- } ) ;
209+ return included ;
210+ }
209211
212+ private List < DocumentData > IncludeRelationshipChain (
213+ List < DocumentData > included , ContextEntity parentEntity , IIdentifiable parentResource , string [ ] relationshipChain , int relationshipChainIndex )
214+ {
215+ var requestedRelationship = relationshipChain [ relationshipChainIndex ] ;
216+ var relationship = parentEntity . Relationships . FirstOrDefault ( r => r . PublicRelationshipName == requestedRelationship ) ;
217+ var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( parentResource , relationship . InternalRelationshipName ) ;
218+ if ( navigationEntity is IEnumerable hasManyNavigationEntity )
219+ {
220+ foreach ( IIdentifiable includedEntity in hasManyNavigationEntity )
221+ {
222+ included = AddIncludedEntity ( included , includedEntity ) ;
223+ included = IncludeSingleResourceRelationships ( included , includedEntity , relationship , relationshipChain , relationshipChainIndex ) ;
224+ }
225+ }
226+ else
227+ {
228+ included = AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
229+ included = IncludeSingleResourceRelationships ( included , ( IIdentifiable ) navigationEntity , relationship , relationshipChain , relationshipChainIndex ) ;
230+ }
231+
232+ return included ;
233+ }
234+
235+ private List < DocumentData > IncludeSingleResourceRelationships (
236+ List < DocumentData > included , IIdentifiable navigationEntity , RelationshipAttribute relationship , string [ ] relationshipChain , int relationshipChainIndex )
237+ {
238+ if ( relationshipChainIndex < relationshipChain . Length )
239+ {
240+ var nextContextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( relationship . Type ) ;
241+ var resource = ( IIdentifiable ) navigationEntity ;
242+ // recursive call
243+ if ( relationshipChainIndex < relationshipChain . Length - 1 )
244+ included = IncludeRelationshipChain ( included , nextContextEntity , resource , relationshipChain , relationshipChainIndex + 1 ) ;
245+ }
246+
210247 return included ;
211248 }
212249
250+
213251 private List < DocumentData > AddIncludedEntity ( List < DocumentData > entities , IIdentifiable entity )
214252 {
215253 var includedEntity = GetIncludedEntity ( entity ) ;
@@ -245,12 +283,6 @@ private DocumentData GetIncludedEntity(IIdentifiable entity)
245283 return data ;
246284 }
247285
248- private bool RelationshipIsIncluded ( string relationshipName )
249- {
250- return _jsonApiContext . IncludedRelationships != null &&
251- _jsonApiContext . IncludedRelationships . Contains ( relationshipName ) ;
252- }
253-
254286 private List < ResourceIdentifierObject > GetRelationships ( IEnumerable < object > entities )
255287 {
256288 var objType = entities . GetElementType ( ) ;
0 commit comments