@@ -33,12 +33,12 @@ public Document Build(IIdentifiable entity)
3333
3434 var document = new Document
3535 {
36- Data = _getData ( contextEntity , entity ) ,
37- Meta = _getMeta ( entity ) ,
36+ Data = GetData ( contextEntity , entity ) ,
37+ Meta = GetMeta ( entity ) ,
3838 Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
3939 } ;
4040
41- document . Included = _appendIncludedObject ( document . Included , contextEntity , entity ) ;
41+ document . Included = AppendIncludedObject ( document . Included , contextEntity , entity ) ;
4242
4343 return document ;
4444 }
@@ -54,20 +54,20 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
5454 var documents = new Documents
5555 {
5656 Data = new List < DocumentData > ( ) ,
57- Meta = _getMeta ( entities . FirstOrDefault ( ) ) ,
57+ Meta = GetMeta ( entities . FirstOrDefault ( ) ) ,
5858 Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
5959 } ;
6060
6161 foreach ( var entity in entities )
6262 {
63- documents . Data . Add ( _getData ( contextEntity , entity ) ) ;
64- documents . Included = _appendIncludedObject ( documents . Included , contextEntity , entity ) ;
63+ documents . Data . Add ( GetData ( contextEntity , entity ) ) ;
64+ documents . Included = AppendIncludedObject ( documents . Included , contextEntity , entity ) ;
6565 }
6666
6767 return documents ;
6868 }
6969
70- private Dictionary < string , object > _getMeta ( IIdentifiable entity )
70+ private Dictionary < string , object > GetMeta ( IIdentifiable entity )
7171 {
7272 if ( entity == null ) return null ;
7373
@@ -87,9 +87,9 @@ private Dictionary<string, object> _getMeta(IIdentifiable entity)
8787 return null ;
8888 }
8989
90- private List < DocumentData > _appendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
90+ private List < DocumentData > AppendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
9191 {
92- var includedEntities = _getIncludedEntities ( contextEntity , entity ) ;
92+ var includedEntities = GetIncludedEntities ( contextEntity , entity ) ;
9393 if ( includedEntities . Count > 0 )
9494 {
9595 if ( includedObject == null )
@@ -100,7 +100,7 @@ private List<DocumentData> _appendIncludedObject(List<DocumentData> includedObje
100100 return includedObject ;
101101 }
102102
103- private DocumentData _getData ( ContextEntity contextEntity , IIdentifiable entity )
103+ private DocumentData GetData ( ContextEntity contextEntity , IIdentifiable entity )
104104 {
105105 var data = new DocumentData
106106 {
@@ -115,16 +115,24 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
115115
116116 contextEntity . Attributes . ForEach ( attr =>
117117 {
118- data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
118+ if ( ShouldIncludeAttribute ( attr ) )
119+ data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
119120 } ) ;
120121
121122 if ( contextEntity . Relationships . Count > 0 )
122- _addRelationships ( data , contextEntity , entity ) ;
123+ AddRelationships ( data , contextEntity , entity ) ;
123124
124125 return data ;
125126 }
126127
127- private void _addRelationships ( DocumentData data , ContextEntity contextEntity , IIdentifiable entity )
128+ private bool ShouldIncludeAttribute ( AttrAttribute attr )
129+ {
130+ return ( _jsonApiContext . QuerySet == null
131+ || _jsonApiContext . QuerySet . Fields . Count == 0
132+ || _jsonApiContext . QuerySet . Fields . Contains ( attr . InternalAttributeName ) ) ;
133+ }
134+
135+ private void AddRelationships ( DocumentData data , ContextEntity contextEntity , IIdentifiable entity )
128136 {
129137 data . Relationships = new Dictionary < string , RelationshipData > ( ) ;
130138 var linkBuilder = new LinkBuilder ( _jsonApiContext ) ;
@@ -140,57 +148,57 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
140148 }
141149 } ;
142150
143- if ( _relationshipIsIncluded ( r . InternalRelationshipName ) )
151+ if ( RelationshipIsIncluded ( r . InternalRelationshipName ) )
144152 {
145153 var navigationEntity = _jsonApiContext . ContextGraph
146154 . GetRelationship ( entity , r . InternalRelationshipName ) ;
147155
148156 if ( navigationEntity == null )
149157 relationshipData . SingleData = null ;
150158 else if ( navigationEntity is IEnumerable )
151- relationshipData . ManyData = _getRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
159+ relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
152160 else
153- relationshipData . SingleData = _getRelationship ( navigationEntity , r . InternalRelationshipName ) ;
161+ relationshipData . SingleData = GetRelationship ( navigationEntity , r . InternalRelationshipName ) ;
154162 }
155163
156164 data . Relationships . Add ( r . InternalRelationshipName . Dasherize ( ) , relationshipData ) ;
157165 } ) ;
158166 }
159167
160- private List < DocumentData > _getIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
168+ private List < DocumentData > GetIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
161169 {
162170 var included = new List < DocumentData > ( ) ;
163171
164172 contextEntity . Relationships . ForEach ( r =>
165173 {
166- if ( ! _relationshipIsIncluded ( r . InternalRelationshipName ) ) return ;
174+ if ( ! RelationshipIsIncluded ( r . InternalRelationshipName ) ) return ;
167175
168176 var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( entity , r . InternalRelationshipName ) ;
169177
170178 if ( navigationEntity is IEnumerable )
171179 foreach ( var includedEntity in ( IEnumerable ) navigationEntity )
172- _addIncludedEntity ( included , ( IIdentifiable ) includedEntity ) ;
180+ AddIncludedEntity ( included , ( IIdentifiable ) includedEntity ) ;
173181 else
174- _addIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
182+ AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
175183 } ) ;
176184
177185 return included ;
178186 }
179187
180- private void _addIncludedEntity ( List < DocumentData > entities , IIdentifiable entity )
188+ private void AddIncludedEntity ( List < DocumentData > entities , IIdentifiable entity )
181189 {
182- var includedEntity = _getIncludedEntity ( entity ) ;
190+ var includedEntity = GetIncludedEntity ( entity ) ;
183191 if ( includedEntity != null )
184192 entities . Add ( includedEntity ) ;
185193 }
186194
187- private DocumentData _getIncludedEntity ( IIdentifiable entity )
195+ private DocumentData GetIncludedEntity ( IIdentifiable entity )
188196 {
189197 if ( entity == null ) return null ;
190198
191199 var contextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) ;
192200
193- var data = _getData ( contextEntity , entity ) ;
201+ var data = GetData ( contextEntity , entity ) ;
194202
195203 data . Attributes = new Dictionary < string , object > ( ) ;
196204
@@ -202,13 +210,13 @@ private DocumentData _getIncludedEntity(IIdentifiable entity)
202210 return data ;
203211 }
204212
205- private bool _relationshipIsIncluded ( string relationshipName )
213+ private bool RelationshipIsIncluded ( string relationshipName )
206214 {
207215 return _jsonApiContext . IncludedRelationships != null &&
208216 _jsonApiContext . IncludedRelationships . Contains ( relationshipName . ToProperCase ( ) ) ;
209217 }
210218
211- private List < Dictionary < string , string > > _getRelationships ( IEnumerable < object > entities , string relationshipName )
219+ private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities , string relationshipName )
212220 {
213221 var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
214222
@@ -224,7 +232,7 @@ private List<Dictionary<string, string>> _getRelationships(IEnumerable<object> e
224232 }
225233 return relationships ;
226234 }
227- private Dictionary < string , string > _getRelationship ( object entity , string relationshipName )
235+ private Dictionary < string , string > GetRelationship ( object entity , string relationshipName )
228236 {
229237 var objType = entity . GetType ( ) ;
230238
0 commit comments