@@ -9,8 +9,8 @@ namespace JsonApiDotNetCore.Builders
99{
1010 public class DocumentBuilder : IDocumentBuilder
1111 {
12- private IJsonApiContext _jsonApiContext ;
13- private IContextGraph _contextGraph ;
12+ private readonly IJsonApiContext _jsonApiContext ;
13+ private readonly IContextGraph _contextGraph ;
1414 private readonly IRequestMeta _requestMeta ;
1515
1616 public DocumentBuilder ( IJsonApiContext jsonApiContext )
@@ -50,14 +50,15 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
5050
5151 var contextEntity = _contextGraph . GetContextEntity ( entityType ) ;
5252
53+ var enumeratedEntities = entities as IList < IIdentifiable > ?? entities . ToList ( ) ;
5354 var documents = new Documents
5455 {
5556 Data = new List < DocumentData > ( ) ,
56- Meta = GetMeta ( entities . FirstOrDefault ( ) ) ,
57+ Meta = GetMeta ( enumeratedEntities . FirstOrDefault ( ) ) ,
5758 Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
5859 } ;
5960
60- foreach ( var entity in entities )
61+ foreach ( var entity in enumeratedEntities )
6162 {
6263 documents . Data . Add ( GetData ( contextEntity , entity ) ) ;
6364 documents . Included = AppendIncludedObject ( documents . Included , contextEntity , entity ) ;
@@ -155,9 +156,9 @@ private void AddRelationships(DocumentData data, ContextEntity contextEntity, II
155156 if ( navigationEntity == null )
156157 relationshipData . SingleData = null ;
157158 else if ( navigationEntity is IEnumerable )
158- relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
159+ relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity ) ;
159160 else
160- relationshipData . SingleData = GetRelationship ( navigationEntity , r . InternalRelationshipName ) ;
161+ relationshipData . SingleData = GetRelationship ( navigationEntity ) ;
161162 }
162163
163164 data . Relationships . Add ( r . PublicRelationshipName , relationshipData ) ;
@@ -174,9 +175,9 @@ private List<DocumentData> GetIncludedEntities(ContextEntity contextEntity, IIde
174175
175176 var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( entity , r . InternalRelationshipName ) ;
176177
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 ) ;
180181 else
181182 AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
182183 } ) ;
@@ -216,7 +217,7 @@ private bool RelationshipIsIncluded(string relationshipName)
216217 _jsonApiContext . IncludedRelationships . Contains ( relationshipName ) ;
217218 }
218219
219- private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities , string relationshipName )
220+ private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities )
220221 {
221222 var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
222223
@@ -232,7 +233,7 @@ private List<Dictionary<string, string>> GetRelationships(IEnumerable<object> en
232233 }
233234 return relationships ;
234235 }
235- private Dictionary < string , string > GetRelationship ( object entity , string relationshipName )
236+ private Dictionary < string , string > GetRelationship ( object entity )
236237 {
237238 var objType = entity . GetType ( ) ;
238239
0 commit comments