@@ -18,8 +18,8 @@ public class DocumentBuilder : IDocumentBuilder
18
18
private readonly IScopedServiceProvider _scopedServiceProvider ;
19
19
20
20
public DocumentBuilder (
21
- IJsonApiContext jsonApiContext ,
22
- IRequestMeta requestMeta = null ,
21
+ IJsonApiContext jsonApiContext ,
22
+ IRequestMeta requestMeta = null ,
23
23
IDocumentBuilderOptionsProvider documentBuilderOptionsProvider = null ,
24
24
IScopedServiceProvider scopedServiceProvider = null )
25
25
{
@@ -112,7 +112,8 @@ public ResourceObject GetData(ContextEntity contextEntity, IIdentifiable entity)
112
112
113
113
public ResourceObject GetData ( ContextEntity contextEntity , IIdentifiable entity , IResourceDefinition resourceDefinition = null )
114
114
{
115
- var data = new ResourceObject {
115
+ var data = new ResourceObject
116
+ {
116
117
Type = contextEntity . EntityName ,
117
118
Id = entity . StringId
118
119
} ;
@@ -177,9 +178,8 @@ private RelationshipData GetRelationshipData(RelationshipAttribute attr, Context
177
178
relationshipData . Links . Related = linkBuilder . GetRelatedRelationLink ( contextEntity . EntityName , entity . StringId , attr . PublicRelationshipName ) ;
178
179
}
179
180
180
- // this only includes the navigation property, we need to actually check the navigation property Id
181
+ // this only includes the navigation property, we need to actually check the navigation property Id
181
182
var navigationEntity = _jsonApiContext . ContextGraph . GetRelationshipValue ( entity , attr ) ;
182
-
183
183
if ( navigationEntity == null )
184
184
relationshipData . SingleData = attr . IsHasOne
185
185
? GetIndependentRelationshipIdentifier ( ( HasOneAttribute ) attr , entity )
@@ -196,14 +196,14 @@ private List<ResourceObject> GetIncludedEntities(List<ResourceObject> included,
196
196
{
197
197
if ( _jsonApiContext . IncludedRelationships != null )
198
198
{
199
- foreach ( var relationshipName in _jsonApiContext . IncludedRelationships )
199
+ foreach ( var relationshipName in _jsonApiContext . IncludedRelationships )
200
200
{
201
201
var relationshipChain = relationshipName . Split ( '.' ) ;
202
202
203
203
var contextEntity = rootContextEntity ;
204
204
var entity = rootResource ;
205
205
included = IncludeRelationshipChain ( included , rootContextEntity , rootResource , relationshipChain , 0 ) ;
206
- }
206
+ }
207
207
}
208
208
209
209
return included ;
@@ -214,7 +214,7 @@ private List<ResourceObject> IncludeRelationshipChain(
214
214
{
215
215
var requestedRelationship = relationshipChain [ relationshipChainIndex ] ;
216
216
var relationship = parentEntity . Relationships . FirstOrDefault ( r => r . PublicRelationshipName == requestedRelationship ) ;
217
- var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( parentResource , relationship . InternalRelationshipName ) ;
217
+ var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( parentResource , relationship . InternalRelationshipName ) ;
218
218
if ( navigationEntity is IEnumerable hasManyNavigationEntity )
219
219
{
220
220
foreach ( IIdentifiable includedEntity in hasManyNavigationEntity )
@@ -227,23 +227,23 @@ private List<ResourceObject> IncludeRelationshipChain(
227
227
{
228
228
included = AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
229
229
included = IncludeSingleResourceRelationships ( included , ( IIdentifiable ) navigationEntity , relationship , relationshipChain , relationshipChainIndex ) ;
230
- }
230
+ }
231
231
232
232
return included ;
233
233
}
234
234
235
235
private List < ResourceObject > IncludeSingleResourceRelationships (
236
236
List < ResourceObject > included , IIdentifiable navigationEntity , RelationshipAttribute relationship , string [ ] relationshipChain , int relationshipChainIndex )
237
237
{
238
- if ( relationshipChainIndex < relationshipChain . Length )
238
+ if ( relationshipChainIndex < relationshipChain . Length )
239
239
{
240
240
var nextContextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( relationship . Type ) ;
241
241
var resource = ( IIdentifiable ) navigationEntity ;
242
242
// recursive call
243
- if ( relationshipChainIndex < relationshipChain . Length - 1 )
243
+ if ( relationshipChainIndex < relationshipChain . Length - 1 )
244
244
included = IncludeRelationshipChain ( included , nextContextEntity , resource , relationshipChain , relationshipChainIndex + 1 ) ;
245
245
}
246
-
246
+
247
247
return included ;
248
248
}
249
249
@@ -289,7 +289,11 @@ private List<ResourceIdentifierObject> GetRelationships(IEnumerable<object> enti
289
289
var relationships = new List < ResourceIdentifierObject > ( ) ;
290
290
foreach ( var entity in entities )
291
291
{
292
- typeName = _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) . EntityName ;
292
+ // this method makes the assumption that entities is a homogenous collection
293
+ // so, we just lookup the type of the first entity on the graph
294
+ // this is better than trying to get it from the generic parameter since it could
295
+ // be less specific than what is registered on the graph (e.g. IEnumerable<object>)
296
+ typeName = typeName ?? _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) . EntityName ;
293
297
relationships . Add ( new ResourceIdentifierObject
294
298
{
295
299
Type = typeName ,
@@ -304,7 +308,7 @@ private ResourceIdentifierObject GetRelationship(object entity)
304
308
var objType = entity . GetType ( ) ;
305
309
var contextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( objType ) ;
306
310
307
- if ( entity is IIdentifiable identifiableEntity )
311
+ if ( entity is IIdentifiable identifiableEntity )
308
312
return new ResourceIdentifierObject
309
313
{
310
314
Type = contextEntity . EntityName ,
0 commit comments