From 25911bc1987130fc507e6ee11daa586ff5f215b0 Mon Sep 17 00:00:00 2001 From: Larry Gebhardt Date: Sun, 15 Mar 2020 12:52:21 -0400 Subject: [PATCH] Fix issue with primary paginator being used for included resources Fixes #1312 --- lib/jsonapi/active_relation_resource.rb | 4 ++-- lib/jsonapi/processor.rb | 8 +++---- test/controllers/controller_test.rb | 31 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/jsonapi/active_relation_resource.rb b/lib/jsonapi/active_relation_resource.rb index d9272339d..e2611613f 100644 --- a/lib/jsonapi/active_relation_resource.rb +++ b/lib/jsonapi/active_relation_resource.rb @@ -395,7 +395,7 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne sort_criteria: sort_criteria, filters: filters) - paginator = options[:paginator] if source_rids.count == 1 + paginator = options[:paginator] records = apply_request_settings_to_records(records: records_for_source_to_related(options), resource_klass: resource_klass, @@ -525,7 +525,7 @@ def find_related_polymorphic_fragments(source_rids, relationship, options, conne relationships: linkage_relationships, filters: filters) - paginator = options[:paginator] if source_rids.count == 1 + paginator = options[:paginator] # Note: We will sort by the source table. Without using unions we can't sort on a polymorphic relationship # in any manner that makes sense diff --git a/lib/jsonapi/processor.rb b/lib/jsonapi/processor.rb index cad9f9b8d..de3459c82 100644 --- a/lib/jsonapi/processor.rb +++ b/lib/jsonapi/processor.rb @@ -392,7 +392,7 @@ def find_related_resource_id_tree(resource_klass, source_id, relationship_name, primary_resource_id_tree = PrimaryResourceIdTree.new primary_resource_id_tree.add_resource_fragments(fragments, include_related) - load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria)) + load_included(resource_klass, primary_resource_id_tree, include_related, options) primary_resource_id_tree end @@ -406,7 +406,7 @@ def find_resource_id_tree(resource_klass, find_options, include_related) primary_resource_id_tree = PrimaryResourceIdTree.new primary_resource_id_tree.add_resource_fragments(fragments, include_related) - load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria)) + load_included(resource_klass, primary_resource_id_tree, include_related, options) primary_resource_id_tree end @@ -422,7 +422,7 @@ def find_resource_id_tree_from_resource_relationship(resource, relationship_name primary_resource_id_tree = PrimaryResourceIdTree.new primary_resource_id_tree.add_resource_fragments(fragments, include_related) - load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria)) + load_included(resource_klass, primary_resource_id_tree, include_related, options) primary_resource_id_tree end @@ -434,7 +434,7 @@ def load_included(resource_klass, source_resource_id_tree, include_related, opti relationship = resource_klass._relationship(key) relationship_name = relationship.name.to_sym - find_related_resource_options = options.dup + find_related_resource_options = options.except(:filters, :sort_criteria, :paginator) find_related_resource_options[:sort_criteria] = relationship.resource_klass.default_sort find_related_resource_options[:cache] = resource_klass.caching? diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index adcac5a94..e815a0e24 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -4752,3 +4752,34 @@ def test_fetch_robots_with_sort_by_version assert_equal 'version is not a valid sort criteria for robots', json_response['errors'].first['detail'] end end + +class Api::V6::AuthorDetailsControllerTest < ActionController::TestCase + def after_teardown + Api::V6::AuthorDetailResource.paginator :none # TODO: ??? + end + + def test_that_the_last_two_author_details_belong_to_an_author + Api::V6::AuthorDetailResource.paginator :offset + + total_count = AuthorDetail.count + assert_operator total_count, :>=, 2 + + assert_cacheable_get :index, params: {sort: :id, include: :author, page: {limit: 10, offset: total_count - 2}} + assert_response :success + assert_equal 2, json_response['data'].size + assert_not_nil json_response['data'][0]['relationships']['author']['data'] + assert_not_nil json_response['data'][1]['relationships']['author']['data'] + end + + def test_that_the_last_author_detail_includes_its_author_even_if_returned_as_the_single_entry_on_a_page_with_nonzero_offset + Api::V6::AuthorDetailResource.paginator :offset + + total_count = AuthorDetail.count + assert_operator total_count, :>=, 2 + + assert_cacheable_get :index, params: {sort: :id, include: :author, page: {limit: 10, offset: total_count - 1}} + assert_response :success + assert_equal 1, json_response['data'].size + assert_not_nil json_response['data'][0]['relationships']['author']['data'] + end +end