diff --git a/lib/jsonapi/processor.rb b/lib/jsonapi/processor.rb index bdd1e13bd..b46601c7a 100644 --- a/lib/jsonapi/processor.rb +++ b/lib/jsonapi/processor.rb @@ -197,7 +197,7 @@ def show_related_resources (paginator && paginator.class.requires_record_count) || (JSONAPI.configuration.top_level_meta_include_page_count)) related_resource_records = source_resource.public_send("records_for_" + relationship_type) - records = resource_klass.filter_records(verified_filters, {}, + records = resource_klass.filter_records(verified_filters, rel_opts, related_resource_records) record_count = resource_klass.count_records(records) diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 6d16ad33d..e5908dd79 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -2754,6 +2754,45 @@ def test_get_related_resources_with_filters end end +class Api::V2::BooksControllerTest < ActionController::TestCase + def test_get_related_resources_with_filters + $test_user = Person.find(5) + original_config = JSONAPI.configuration.dup + JSONAPI.configuration.top_level_meta_include_record_count = true + JSONAPI.configuration.json_key_format = :dasherized_key + assert_cacheable_get :get_related_resources, + params: { + author_id: '1', + relationship: 'books', + source: 'api/v2/authors', + filter: { fiction: 'true' } + } + assert_response :success + assert_equal 1, json_response['meta']['record-count'] + ensure + JSONAPI.configuration = original_config + end + + def test_get_related_resources_with_filters_2 + # Admin user can find an author's banned books + $test_user = Person.find(5) + original_config = JSONAPI.configuration.dup + JSONAPI.configuration.top_level_meta_include_record_count = true + JSONAPI.configuration.json_key_format = :dasherized_key + assert_cacheable_get :get_related_resources, + params: { + author_id: '1', + relationship: 'books', + source: 'api/v2/authors', + filter: { banned: 'true' } + } + assert_response :success + assert_equal 1, json_response['meta']['record-count'] + ensure + JSONAPI.configuration = original_config + end +end + class BreedsControllerTest < ActionController::TestCase # Note: Breed names go through the TitleValueFormatter diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index f2f38dd5e..2a43a97bc 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -1552,6 +1552,8 @@ def apply_filter_banned(records, value, options) # Only book admins might filter for banned books if current_user && current_user.book_admin records.where('books.banned = ?', value[0] == 'true') + else + records end end