@@ -9,8 +9,8 @@ namespace JsonApiDotNetCore.Builders
9
9
{
10
10
public class DocumentBuilder : IDocumentBuilder
11
11
{
12
- private IJsonApiContext _jsonApiContext ;
13
- private IContextGraph _contextGraph ;
12
+ private readonly IJsonApiContext _jsonApiContext ;
13
+ private readonly IContextGraph _contextGraph ;
14
14
private readonly IRequestMeta _requestMeta ;
15
15
16
16
public DocumentBuilder ( IJsonApiContext jsonApiContext )
@@ -50,14 +50,15 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
50
50
51
51
var contextEntity = _contextGraph . GetContextEntity ( entityType ) ;
52
52
53
+ var enumeratedEntities = entities as IList < IIdentifiable > ?? entities . ToList ( ) ;
53
54
var documents = new Documents
54
55
{
55
56
Data = new List < DocumentData > ( ) ,
56
- Meta = GetMeta ( entities . FirstOrDefault ( ) ) ,
57
+ Meta = GetMeta ( enumeratedEntities . FirstOrDefault ( ) ) ,
57
58
Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
58
59
} ;
59
60
60
- foreach ( var entity in entities )
61
+ foreach ( var entity in enumeratedEntities )
61
62
{
62
63
documents . Data . Add ( GetData ( contextEntity , entity ) ) ;
63
64
documents . Included = AppendIncludedObject ( documents . Included , contextEntity , entity ) ;
@@ -155,9 +156,9 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
155
156
if ( navigationEntity == null )
156
157
relationshipData . SingleData = null ;
157
158
else if ( navigationEntity is IEnumerable )
158
- relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
159
+ relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity ) ;
159
160
else
160
- relationshipData . SingleData = GetRelationship ( navigationEntity , r . InternalRelationshipName ) ;
161
+ relationshipData . SingleData = GetRelationship ( navigationEntity ) ;
161
162
}
162
163
163
164
data . Relationships . Add ( r . PublicRelationshipName , relationshipData ) ;
@@ -174,9 +175,9 @@ private List<DocumentData> GetIncludedEntities(ContextEntity contextEntity, IIde
174
175
175
176
var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( entity , r . InternalRelationshipName ) ;
176
177
177
- if ( navigationEntity is IEnumerable )
178
- foreach ( var includedEntity in ( IEnumerable ) navigationEntity )
179
- AddIncludedEntity ( included , ( IIdentifiable ) includedEntity ) ;
178
+ if ( navigationEntity is IEnumerable hasManyNavigationEntity )
179
+ foreach ( IIdentifiable includedEntity in hasManyNavigationEntity )
180
+ AddIncludedEntity ( included , includedEntity ) ;
180
181
else
181
182
AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
182
183
} ) ;
@@ -216,7 +217,7 @@ private bool RelationshipIsIncluded(string relationshipName)
216
217
_jsonApiContext . IncludedRelationships . Contains ( relationshipName ) ;
217
218
}
218
219
219
- private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities , string relationshipName )
220
+ private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities )
220
221
{
221
222
var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
222
223
@@ -232,7 +233,7 @@ private List<Dictionary<string, string>> GetRelationships(IEnumerable<object> en
232
233
}
233
234
return relationships ;
234
235
}
235
- private Dictionary < string , string > GetRelationship ( object entity , string relationshipName )
236
+ private Dictionary < string , string > GetRelationship ( object entity )
236
237
{
237
238
var objType = entity . GetType ( ) ;
238
239
0 commit comments