@@ -138,13 +138,15 @@ public ResourceObject GetData(ContextEntity contextEntity, IIdentifiable entity,
138138
139139 return data ;
140140 }
141- private bool ShouldIncludeAttribute ( AttrAttribute attr , object attributeValue )
141+ private bool ShouldIncludeAttribute ( AttrAttribute attr , object attributeValue , RelationshipAttribute relationship = null )
142142 {
143143 return OmitNullValuedAttribute ( attr , attributeValue ) == false
144144 && attr . InternalAttributeName != nameof ( Identifiable . Id )
145145 && ( ( _jsonApiContext . QuerySet == null
146146 || _jsonApiContext . QuerySet . Fields . Count == 0 )
147- || _jsonApiContext . QuerySet . Fields . Contains ( attr . InternalAttributeName ) ) ;
147+ || _jsonApiContext . QuerySet . Fields . Contains ( relationship != null ?
148+ $ "{ relationship . InternalRelationshipName } .{ attr . InternalAttributeName } " :
149+ attr . InternalAttributeName ) ) ;
148150 }
149151
150152 private bool OmitNullValuedAttribute ( AttrAttribute attr , object attributeValue )
@@ -225,13 +227,13 @@ private List<ResourceObject> IncludeRelationshipChain(
225227 {
226228 foreach ( IIdentifiable includedEntity in hasManyNavigationEntity )
227229 {
228- included = AddIncludedEntity ( included , includedEntity ) ;
230+ included = AddIncludedEntity ( included , includedEntity , relationship ) ;
229231 included = IncludeSingleResourceRelationships ( included , includedEntity , relationship , relationshipChain , relationshipChainIndex ) ;
230232 }
231233 }
232234 else
233235 {
234- included = AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
236+ included = AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity , relationship ) ;
235237 included = IncludeSingleResourceRelationships ( included , ( IIdentifiable ) navigationEntity , relationship , relationshipChain , relationshipChainIndex ) ;
236238 }
237239
@@ -254,9 +256,9 @@ private List<ResourceObject> IncludeSingleResourceRelationships(
254256 }
255257
256258
257- private List < ResourceObject > AddIncludedEntity ( List < ResourceObject > entities , IIdentifiable entity )
259+ private List < ResourceObject > AddIncludedEntity ( List < ResourceObject > entities , IIdentifiable entity , RelationshipAttribute relationship )
258260 {
259- var includedEntity = GetIncludedEntity ( entity ) ;
261+ var includedEntity = GetIncludedEntity ( entity , relationship ) ;
260262
261263 if ( entities == null )
262264 entities = new List < ResourceObject > ( ) ;
@@ -270,7 +272,7 @@ private List<ResourceObject> AddIncludedEntity(List<ResourceObject> entities, II
270272 return entities ;
271273 }
272274
273- private ResourceObject GetIncludedEntity ( IIdentifiable entity )
275+ private ResourceObject GetIncludedEntity ( IIdentifiable entity , RelationshipAttribute relationship )
274276 {
275277 if ( entity == null ) return null ;
276278
@@ -281,13 +283,14 @@ private ResourceObject GetIncludedEntity(IIdentifiable entity)
281283
282284 data . Attributes = new Dictionary < string , object > ( ) ;
283285
284- foreach ( var attr in contextEntity . Attributes )
286+ contextEntity . Attributes . ForEach ( attr =>
285287 {
286- if ( attr . InternalAttributeName == nameof ( Identifiable . Id ) )
287- continue ;
288-
289- data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
290- }
288+ var attributeValue = attr . GetValue ( entity ) ;
289+ if ( ShouldIncludeAttribute ( attr , attributeValue , relationship ) )
290+ {
291+ data . Attributes . Add ( attr . PublicAttributeName , attributeValue ) ;
292+ }
293+ } ) ;
291294
292295 return data ;
293296 }
0 commit comments