Skip to content

Commit 0623552

Browse files
aribouiusrichmolj
authored andcommitted
Allow using sparse fieldsets and extra fields (#77)
- Modifies the RenderOptions class to support using both sparse fieldsets and extra fields by merging the two when both present
1 parent dafe03f commit 0623552

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

lib/jsonapi_compliable/util/render_options.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ def self.generate(object, query_hash, overrides = {})
1212
h[k] = [*names, "Serializable#{klass}"].join('::').safe_constantize
1313
end
1414

15+
fields = query_hash[:fields].dup
16+
extra_fields = query_hash[:extra_fields]
17+
18+
if extra_fields.any? && fields.any?
19+
extra_fields.each { |k,v| fields[k] = fields[k].to_a + v }
20+
end
21+
1522
options = {}
1623
options[:class] = inferrer
1724
options[:include] = query_hash[:include]
1825
options[:jsonapi] = resolved
19-
options[:fields] = query_hash[:fields]
26+
options[:fields] = fields
2027
options.merge!(overrides)
2128
options[:meta] ||= {}
2229
options[:expose] ||= {}
23-
options[:expose][:extra_fields] = query_hash[:extra_fields]
30+
options[:expose][:extra_fields] = extra_fields
2431

2532
if object.respond_to?(:resolve_stats)
2633
stats = object.resolve_stats

spec/integration/rails/finders_spec.rb

+48
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)