Skip to content

Commit

Permalink
Merge pull request #1317 from cerebris/issue_1312
Browse files Browse the repository at this point in the history
Fix issue with primary paginator being used for included resources
  • Loading branch information
lgebhardt authored Jul 10, 2020
2 parents 7b64a08 + 25911bc commit 5517f17
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/jsonapi/active_relation_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/jsonapi/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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?

Expand Down
31 changes: 31 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5517f17

Please sign in to comment.