@@ -207,6 +207,14 @@ def json
207
207
expect ( book ) . to_not have_key ( 'alternate_title' )
208
208
expect ( book [ 'pages' ] ) . to eq ( 500 )
209
209
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
210
218
end
211
219
212
220
context 'sideloading belongs_to' do
@@ -225,6 +233,14 @@ def json
225
233
expect ( state ) . to_not have_key ( 'abbreviation' )
226
234
expect ( state ) . to_not have_key ( 'population' )
227
235
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
228
244
end
229
245
230
246
context 'sideloading has_one' do
@@ -243,6 +259,14 @@ def json
243
259
expect ( bio ) . to_not have_key ( 'created_at' )
244
260
expect ( bio ) . to_not have_key ( 'picture' )
245
261
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
246
270
end
247
271
248
272
context 'sideloading has_and_belongs_to_many' do
@@ -272,6 +296,14 @@ def json
272
296
expect ( hobby ) . to_not have_key ( 'reason' )
273
297
end
274
298
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
+
275
307
it 'does not duplicate results' do
276
308
get :index , params : { include : 'hobbies' }
277
309
author1_relationships = json [ 'data' ] [ 0 ] [ 'relationships' ]
@@ -355,6 +387,22 @@ def json
355
387
expect ( condo ) . to_not have_key ( 'condo_price' )
356
388
end
357
389
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
+
358
406
it 'allows additional levels of nesting' do
359
407
get :index , params : { include : 'dwelling.state' }
360
408
expect ( json_includes ( 'states' ) . length ) . to eq ( 1 )
0 commit comments