Skip to content

Commit 10b4bf8

Browse files
Truman ShuckCarl Thuringer
authored andcommitted
Allow included resources to be filtered when they are related with the 'eager_load_on_include: false' flag set
1 parent 271ee11 commit 10b4bf8

4 files changed

Lines changed: 21 additions & 2 deletions

File tree

lib/jsonapi/request_parser.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ def parse_filters(filters)
257257
if included_resource_name
258258
relationship = resource_klass._relationship(included_resource_name || '')
259259

260-
261260
unless relationship
262261
return @errors.concat(Exceptions::FilterNotAllowed.new(filter_method).errors)
263262
end
@@ -266,7 +265,7 @@ def parse_filters(filters)
266265
return @errors.concat(Exceptions::FilterNotAllowed.new(filter_method).errors)
267266
end
268267

269-
unless @include_directives.model_includes.include?(relationship.name.to_sym)
268+
unless @include_directives.include_config(relationship.name.to_sym).present?
270269
return @errors.concat(Exceptions::FilterNotAllowed.new(filter_method).errors)
271270
end
272271

lib/jsonapi/resource.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,9 @@ def apply_included_resources_filters(records, options = {})
741741
next memo unless relationship && relationship.is_a?(JSONAPI::Relationship::ToMany)
742742
filtering_resource = relationship.resource_klass
743743

744+
# Don't try to merge where clauses when relation isn't already being joined to query.
745+
next memo unless config[:include_in_join]
746+
744747
filters = config[:include_filters]
745748
next memo unless filters
746749

test/controllers/controller_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,22 @@ def test_show_single_with_includes
509509
assert_equal 2, json_response['included'].size
510510
end
511511

512+
def test_show_with_filtered_includes_when_not_eager_loaded
513+
# tags are not eagerly loaded on a post but may still be filtered
514+
get :show, params: { id: '1', include: 'tags', filter: { 'tags.name' => ['whiny'] } }
515+
assert_response :success
516+
assert_equal 1, json_response['included'].size
517+
end
518+
519+
def test_show_with_filtered_includes_when_not_eager_loaded_and_all_filtered_out
520+
get :show, params: { id: '1', include: 'tags', filter: { 'tags.name' => ['no-tag-with-this-name'] } }
521+
assert_response :success
522+
assert json_response['data'].is_a?(Hash)
523+
assert_equal 'New post', json_response['data']['attributes']['title']
524+
assert_equal 'A body!!!', json_response['data']['attributes']['body']
525+
assert_nil json_response['included']
526+
end
527+
512528
def test_show_single_with_include_disallowed
513529
JSONAPI.configuration.allow_include = false
514530
assert_cacheable_get :show, params: {id: '1', include: 'comments'}

test/fixtures/active_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ class TagResource < JSONAPI::Resource
10551055
attributes :name
10561056

10571057
has_many :posts
1058+
filter :name
10581059
# Not including the planets relationship so they don't get output
10591060
#has_many :planets
10601061
end

0 commit comments

Comments
 (0)