Skip to content

Commit ee7aac5

Browse files
hatch-carllgebhardt
authored andcommitted
Pass rel_opts when filtering records for record count
Naturally I uncovered this because it caused a more complex bug in my app. The problem here is that if you have a filter which depends on something that is set in, for example, the context, you will not have that context when the filter is re-invoked for record count, which happens when the record count configuration is active. This only happens when fetching related resources. In this case I add a test where we try to fetch an author's banned books, of which there should be only one. However, because the prior code discards the rel_opts and instead provides an empty hash (!) the filter does not execute because it will only operate when an admin user is in the context. Closes gh-1234
1 parent f6e3183 commit ee7aac5

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/jsonapi/processor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def show_related_resources
197197
(paginator && paginator.class.requires_record_count) ||
198198
(JSONAPI.configuration.top_level_meta_include_page_count))
199199
related_resource_records = source_resource.public_send("records_for_" + relationship_type)
200-
records = resource_klass.filter_records(verified_filters, {},
200+
records = resource_klass.filter_records(verified_filters, rel_opts,
201201
related_resource_records)
202202

203203
record_count = resource_klass.count_records(records)

test/controllers/controller_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2754,6 +2754,45 @@ def test_get_related_resources_with_filters
27542754
end
27552755
end
27562756

2757+
class Api::V2::BooksControllerTest < ActionController::TestCase
2758+
def test_get_related_resources_with_filters
2759+
$test_user = Person.find(5)
2760+
original_config = JSONAPI.configuration.dup
2761+
JSONAPI.configuration.top_level_meta_include_record_count = true
2762+
JSONAPI.configuration.json_key_format = :dasherized_key
2763+
assert_cacheable_get :get_related_resources,
2764+
params: {
2765+
author_id: '1',
2766+
relationship: 'books',
2767+
source: 'api/v2/authors',
2768+
filter: { fiction: 'true' }
2769+
}
2770+
assert_response :success
2771+
assert_equal 1, json_response['meta']['record-count']
2772+
ensure
2773+
JSONAPI.configuration = original_config
2774+
end
2775+
2776+
def test_get_related_resources_with_filters_2
2777+
# Admin user can find an author's banned books
2778+
$test_user = Person.find(5)
2779+
original_config = JSONAPI.configuration.dup
2780+
JSONAPI.configuration.top_level_meta_include_record_count = true
2781+
JSONAPI.configuration.json_key_format = :dasherized_key
2782+
assert_cacheable_get :get_related_resources,
2783+
params: {
2784+
author_id: '1',
2785+
relationship: 'books',
2786+
source: 'api/v2/authors',
2787+
filter: { banned: 'true' }
2788+
}
2789+
assert_response :success
2790+
assert_equal 1, json_response['meta']['record-count']
2791+
ensure
2792+
JSONAPI.configuration = original_config
2793+
end
2794+
end
2795+
27572796
class BreedsControllerTest < ActionController::TestCase
27582797
# Note: Breed names go through the TitleValueFormatter
27592798

test/fixtures/active_record.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,8 @@ def apply_filter_banned(records, value, options)
15521552
# Only book admins might filter for banned books
15531553
if current_user && current_user.book_admin
15541554
records.where('books.banned = ?', value[0] == 'true')
1555+
else
1556+
records
15551557
end
15561558
end
15571559

0 commit comments

Comments
 (0)