@@ -207,6 +207,14 @@ def json
207207 expect ( book ) . to_not have_key ( 'alternate_title' )
208208 expect ( book [ 'pages' ] ) . to eq ( 500 )
209209 end
210+
211+ it 'allows extra fields and sparse fieldsets for the sideloaded resource' do
212+ get :index , params : { include : 'books' , fields : { books : 'pages' } , extra_fields : { books : 'alternate_title' } }
213+ book = json_includes ( 'books' ) [ 0 ] [ 'attributes' ]
214+ expect ( book ) . to have_key ( 'pages' )
215+ expect ( book ) . to have_key ( 'alternate_title' )
216+ expect ( book ) . to_not have_key ( 'title' )
217+ end
210218 end
211219
212220 context 'sideloading belongs_to' do
@@ -225,6 +233,14 @@ def json
225233 expect ( state ) . to_not have_key ( 'abbreviation' )
226234 expect ( state ) . to_not have_key ( 'population' )
227235 end
236+
237+ it 'allows extra fields and sparse fieldsets for the sideloaded resource' do
238+ get :index , params : { include : 'state' , fields : { states : 'name' } , extra_fields : { states : 'population' } }
239+ state = json_includes ( 'states' ) [ 0 ] [ 'attributes' ]
240+ expect ( state ) . to have_key ( 'name' )
241+ expect ( state ) . to have_key ( 'population' )
242+ expect ( state ) . to_not have_key ( 'abbreviation' )
243+ end
228244 end
229245
230246 context 'sideloading has_one' do
@@ -243,6 +259,14 @@ def json
243259 expect ( bio ) . to_not have_key ( 'created_at' )
244260 expect ( bio ) . to_not have_key ( 'picture' )
245261 end
262+
263+ it 'allows extra fields and sparse fieldsets for the sideloaded resource' do
264+ get :index , params : { include : 'bio' , fields : { bios : 'description' } , extra_fields : { bios : 'created_at' } }
265+ bio = json_includes ( 'bios' ) [ 0 ] [ 'attributes' ]
266+ expect ( bio ) . to have_key ( 'description' )
267+ expect ( bio ) . to have_key ( 'created_at' )
268+ expect ( bio ) . to_not have_key ( 'picture' )
269+ end
246270 end
247271
248272 context 'sideloading has_and_belongs_to_many' do
@@ -272,6 +296,14 @@ def json
272296 expect ( hobby ) . to_not have_key ( 'reason' )
273297 end
274298
299+ it 'allows extra fields and sparse fieldsets for the sideloaded resource' do
300+ get :index , params : { include : 'hobbies' , fields : { hobbies : 'name' } , extra_fields : { hobbies : 'reason' } }
301+ hobby = json_includes ( 'hobbies' ) [ 0 ] [ 'attributes' ]
302+ expect ( hobby ) . to have_key ( 'name' )
303+ expect ( hobby ) . to have_key ( 'reason' )
304+ expect ( hobby ) . to_not have_key ( 'description' )
305+ end
306+
275307 it 'does not duplicate results' do
276308 get :index , params : { include : 'hobbies' }
277309 author1_relationships = json [ 'data' ] [ 0 ] [ 'relationships' ]
@@ -355,6 +387,22 @@ def json
355387 expect ( condo ) . to_not have_key ( 'condo_price' )
356388 end
357389
390+ it 'allows extra fields and sparse fieldsets for the sideloaded resource' do
391+ get :index , params : {
392+ include : 'dwelling' ,
393+ fields : { houses : 'name' , condos : 'condo_description' } ,
394+ extra_fields : { houses : 'house_price' , condos : 'condo_price' }
395+ }
396+ house = json_includes ( 'houses' ) [ 0 ] [ 'attributes' ]
397+ condo = json_includes ( 'condos' ) [ 0 ] [ 'attributes' ]
398+ expect ( house ) . to have_key ( 'name' )
399+ expect ( house ) . to have_key ( 'house_price' )
400+ expect ( house ) . to_not have_key ( 'house_description' )
401+ expect ( condo ) . to have_key ( 'condo_description' )
402+ expect ( condo ) . to have_key ( 'condo_price' )
403+ expect ( condo ) . to_not have_key ( 'name' )
404+ end
405+
358406 it 'allows additional levels of nesting' do
359407 get :index , params : { include : 'dwelling.state' }
360408 expect ( json_includes ( 'states' ) . length ) . to eq ( 1 )
0 commit comments