@@ -192,24 +192,62 @@ private RelationshipData GetRelationshipData(RelationshipAttribute attr, Context
192
192
return relationshipData ;
193
193
}
194
194
195
- private List < DocumentData > GetIncludedEntities ( List < DocumentData > included , ContextEntity contextEntity , IIdentifiable entity )
195
+ private List < DocumentData > GetIncludedEntities ( List < DocumentData > included , ContextEntity rootContextEntity , IIdentifiable rootResource )
196
196
{
197
- contextEntity . Relationships . ForEach ( r =>
197
+ if ( _jsonApiContext . IncludedRelationships != null )
198
198
{
199
- if ( ! RelationshipIsIncluded ( r . PublicRelationshipName ) ) return ;
199
+ foreach ( var relationshipName in _jsonApiContext . IncludedRelationships )
200
+ {
201
+ var relationshipChain = relationshipName . Split ( '.' ) ;
200
202
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
+ }
202
208
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
+ }
209
211
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
+
210
247
return included ;
211
248
}
212
249
250
+
213
251
private List < DocumentData > AddIncludedEntity ( List < DocumentData > entities , IIdentifiable entity )
214
252
{
215
253
var includedEntity = GetIncludedEntity ( entity ) ;
@@ -245,12 +283,6 @@ private DocumentData GetIncludedEntity(IIdentifiable entity)
245
283
return data ;
246
284
}
247
285
248
- private bool RelationshipIsIncluded ( string relationshipName )
249
- {
250
- return _jsonApiContext . IncludedRelationships != null &&
251
- _jsonApiContext . IncludedRelationships . Contains ( relationshipName ) ;
252
- }
253
-
254
286
private List < ResourceIdentifierObject > GetRelationships ( IEnumerable < object > entities )
255
287
{
256
288
var objType = entities . GetElementType ( ) ;
0 commit comments