@@ -33,12 +33,12 @@ public Document Build(IIdentifiable entity)
33
33
34
34
var document = new Document
35
35
{
36
- Data = _getData ( contextEntity , entity ) ,
37
- Meta = _getMeta ( entity ) ,
36
+ Data = GetData ( contextEntity , entity ) ,
37
+ Meta = GetMeta ( entity ) ,
38
38
Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
39
39
} ;
40
40
41
- document . Included = _appendIncludedObject ( document . Included , contextEntity , entity ) ;
41
+ document . Included = AppendIncludedObject ( document . Included , contextEntity , entity ) ;
42
42
43
43
return document ;
44
44
}
@@ -54,20 +54,20 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
54
54
var documents = new Documents
55
55
{
56
56
Data = new List < DocumentData > ( ) ,
57
- Meta = _getMeta ( entities . FirstOrDefault ( ) ) ,
57
+ Meta = GetMeta ( entities . FirstOrDefault ( ) ) ,
58
58
Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
59
59
} ;
60
60
61
61
foreach ( var entity in entities )
62
62
{
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 ) ;
65
65
}
66
66
67
67
return documents ;
68
68
}
69
69
70
- private Dictionary < string , object > _getMeta ( IIdentifiable entity )
70
+ private Dictionary < string , object > GetMeta ( IIdentifiable entity )
71
71
{
72
72
if ( entity == null ) return null ;
73
73
@@ -87,9 +87,9 @@ private Dictionary<string, object> _getMeta(IIdentifiable entity)
87
87
return null ;
88
88
}
89
89
90
- private List < DocumentData > _appendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
90
+ private List < DocumentData > AppendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
91
91
{
92
- var includedEntities = _getIncludedEntities ( contextEntity , entity ) ;
92
+ var includedEntities = GetIncludedEntities ( contextEntity , entity ) ;
93
93
if ( includedEntities . Count > 0 )
94
94
{
95
95
if ( includedObject == null )
@@ -100,7 +100,7 @@ private List<DocumentData> _appendIncludedObject(List<DocumentData> includedObje
100
100
return includedObject ;
101
101
}
102
102
103
- private DocumentData _getData ( ContextEntity contextEntity , IIdentifiable entity )
103
+ private DocumentData GetData ( ContextEntity contextEntity , IIdentifiable entity )
104
104
{
105
105
var data = new DocumentData
106
106
{
@@ -115,16 +115,24 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
115
115
116
116
contextEntity . Attributes . ForEach ( attr =>
117
117
{
118
- data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
118
+ if ( ShouldIncludeAttribute ( attr ) )
119
+ data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
119
120
} ) ;
120
121
121
122
if ( contextEntity . Relationships . Count > 0 )
122
- _addRelationships ( data , contextEntity , entity ) ;
123
+ AddRelationships ( data , contextEntity , entity ) ;
123
124
124
125
return data ;
125
126
}
126
127
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 )
128
136
{
129
137
data . Relationships = new Dictionary < string , RelationshipData > ( ) ;
130
138
var linkBuilder = new LinkBuilder ( _jsonApiContext ) ;
@@ -140,57 +148,57 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
140
148
}
141
149
} ;
142
150
143
- if ( _relationshipIsIncluded ( r . InternalRelationshipName ) )
151
+ if ( RelationshipIsIncluded ( r . InternalRelationshipName ) )
144
152
{
145
153
var navigationEntity = _jsonApiContext . ContextGraph
146
154
. GetRelationship ( entity , r . InternalRelationshipName ) ;
147
155
148
156
if ( navigationEntity == null )
149
157
relationshipData . SingleData = null ;
150
158
else if ( navigationEntity is IEnumerable )
151
- relationshipData . ManyData = _getRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
159
+ relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
152
160
else
153
- relationshipData . SingleData = _getRelationship ( navigationEntity , r . InternalRelationshipName ) ;
161
+ relationshipData . SingleData = GetRelationship ( navigationEntity , r . InternalRelationshipName ) ;
154
162
}
155
163
156
164
data . Relationships . Add ( r . InternalRelationshipName . Dasherize ( ) , relationshipData ) ;
157
165
} ) ;
158
166
}
159
167
160
- private List < DocumentData > _getIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
168
+ private List < DocumentData > GetIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
161
169
{
162
170
var included = new List < DocumentData > ( ) ;
163
171
164
172
contextEntity . Relationships . ForEach ( r =>
165
173
{
166
- if ( ! _relationshipIsIncluded ( r . InternalRelationshipName ) ) return ;
174
+ if ( ! RelationshipIsIncluded ( r . InternalRelationshipName ) ) return ;
167
175
168
176
var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( entity , r . InternalRelationshipName ) ;
169
177
170
178
if ( navigationEntity is IEnumerable )
171
179
foreach ( var includedEntity in ( IEnumerable ) navigationEntity )
172
- _addIncludedEntity ( included , ( IIdentifiable ) includedEntity ) ;
180
+ AddIncludedEntity ( included , ( IIdentifiable ) includedEntity ) ;
173
181
else
174
- _addIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
182
+ AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
175
183
} ) ;
176
184
177
185
return included ;
178
186
}
179
187
180
- private void _addIncludedEntity ( List < DocumentData > entities , IIdentifiable entity )
188
+ private void AddIncludedEntity ( List < DocumentData > entities , IIdentifiable entity )
181
189
{
182
- var includedEntity = _getIncludedEntity ( entity ) ;
190
+ var includedEntity = GetIncludedEntity ( entity ) ;
183
191
if ( includedEntity != null )
184
192
entities . Add ( includedEntity ) ;
185
193
}
186
194
187
- private DocumentData _getIncludedEntity ( IIdentifiable entity )
195
+ private DocumentData GetIncludedEntity ( IIdentifiable entity )
188
196
{
189
197
if ( entity == null ) return null ;
190
198
191
199
var contextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) ;
192
200
193
- var data = _getData ( contextEntity , entity ) ;
201
+ var data = GetData ( contextEntity , entity ) ;
194
202
195
203
data . Attributes = new Dictionary < string , object > ( ) ;
196
204
@@ -202,13 +210,13 @@ private DocumentData _getIncludedEntity(IIdentifiable entity)
202
210
return data ;
203
211
}
204
212
205
- private bool _relationshipIsIncluded ( string relationshipName )
213
+ private bool RelationshipIsIncluded ( string relationshipName )
206
214
{
207
215
return _jsonApiContext . IncludedRelationships != null &&
208
216
_jsonApiContext . IncludedRelationships . Contains ( relationshipName . ToProperCase ( ) ) ;
209
217
}
210
218
211
- private List < Dictionary < string , string > > _getRelationships ( IEnumerable < object > entities , string relationshipName )
219
+ private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities , string relationshipName )
212
220
{
213
221
var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
214
222
@@ -224,7 +232,7 @@ private List<Dictionary<string, string>> _getRelationships(IEnumerable<object> e
224
232
}
225
233
return relationships ;
226
234
}
227
- private Dictionary < string , string > _getRelationship ( object entity , string relationshipName )
235
+ private Dictionary < string , string > GetRelationship ( object entity , string relationshipName )
228
236
{
229
237
var objType = entity . GetType ( ) ;
230
238
0 commit comments