Skip to content

Commit 5517f17

Browse files
authored
Merge pull request #1317 from cerebris/issue_1312
Fix issue with primary paginator being used for included resources
2 parents 7b64a08 + 25911bc commit 5517f17

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

lib/jsonapi/active_relation_resource.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne
395395
sort_criteria: sort_criteria,
396396
filters: filters)
397397

398-
paginator = options[:paginator] if source_rids.count == 1
398+
paginator = options[:paginator]
399399

400400
records = apply_request_settings_to_records(records: records_for_source_to_related(options),
401401
resource_klass: resource_klass,
@@ -525,7 +525,7 @@ def find_related_polymorphic_fragments(source_rids, relationship, options, conne
525525
relationships: linkage_relationships,
526526
filters: filters)
527527

528-
paginator = options[:paginator] if source_rids.count == 1
528+
paginator = options[:paginator]
529529

530530
# Note: We will sort by the source table. Without using unions we can't sort on a polymorphic relationship
531531
# in any manner that makes sense

lib/jsonapi/processor.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def find_related_resource_id_tree(resource_klass, source_id, relationship_name,
392392
primary_resource_id_tree = PrimaryResourceIdTree.new
393393
primary_resource_id_tree.add_resource_fragments(fragments, include_related)
394394

395-
load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria))
395+
load_included(resource_klass, primary_resource_id_tree, include_related, options)
396396

397397
primary_resource_id_tree
398398
end
@@ -406,7 +406,7 @@ def find_resource_id_tree(resource_klass, find_options, include_related)
406406
primary_resource_id_tree = PrimaryResourceIdTree.new
407407
primary_resource_id_tree.add_resource_fragments(fragments, include_related)
408408

409-
load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria))
409+
load_included(resource_klass, primary_resource_id_tree, include_related, options)
410410

411411
primary_resource_id_tree
412412
end
@@ -422,7 +422,7 @@ def find_resource_id_tree_from_resource_relationship(resource, relationship_name
422422
primary_resource_id_tree = PrimaryResourceIdTree.new
423423
primary_resource_id_tree.add_resource_fragments(fragments, include_related)
424424

425-
load_included(resource_klass, primary_resource_id_tree, include_related, options.except(:filters, :sort_criteria))
425+
load_included(resource_klass, primary_resource_id_tree, include_related, options)
426426

427427
primary_resource_id_tree
428428
end
@@ -434,7 +434,7 @@ def load_included(resource_klass, source_resource_id_tree, include_related, opti
434434
relationship = resource_klass._relationship(key)
435435
relationship_name = relationship.name.to_sym
436436

437-
find_related_resource_options = options.dup
437+
find_related_resource_options = options.except(:filters, :sort_criteria, :paginator)
438438
find_related_resource_options[:sort_criteria] = relationship.resource_klass.default_sort
439439
find_related_resource_options[:cache] = resource_klass.caching?
440440

test/controllers/controller_test.rb

+31
Original file line numberDiff line numberDiff line change
@@ -4752,3 +4752,34 @@ def test_fetch_robots_with_sort_by_version
47524752
assert_equal 'version is not a valid sort criteria for robots', json_response['errors'].first['detail']
47534753
end
47544754
end
4755+
4756+
class Api::V6::AuthorDetailsControllerTest < ActionController::TestCase
4757+
def after_teardown
4758+
Api::V6::AuthorDetailResource.paginator :none # TODO: ???
4759+
end
4760+
4761+
def test_that_the_last_two_author_details_belong_to_an_author
4762+
Api::V6::AuthorDetailResource.paginator :offset
4763+
4764+
total_count = AuthorDetail.count
4765+
assert_operator total_count, :>=, 2
4766+
4767+
assert_cacheable_get :index, params: {sort: :id, include: :author, page: {limit: 10, offset: total_count - 2}}
4768+
assert_response :success
4769+
assert_equal 2, json_response['data'].size
4770+
assert_not_nil json_response['data'][0]['relationships']['author']['data']
4771+
assert_not_nil json_response['data'][1]['relationships']['author']['data']
4772+
end
4773+
4774+
def test_that_the_last_author_detail_includes_its_author_even_if_returned_as_the_single_entry_on_a_page_with_nonzero_offset
4775+
Api::V6::AuthorDetailResource.paginator :offset
4776+
4777+
total_count = AuthorDetail.count
4778+
assert_operator total_count, :>=, 2
4779+
4780+
assert_cacheable_get :index, params: {sort: :id, include: :author, page: {limit: 10, offset: total_count - 1}}
4781+
assert_response :success
4782+
assert_equal 1, json_response['data'].size
4783+
assert_not_nil json_response['data'][0]['relationships']['author']['data']
4784+
end
4785+
end

0 commit comments

Comments
 (0)