Skip to content

Commit 9e2a8c1

Browse files
hstrowdlgebhardt
authored andcommitted
Corrects association fetching logic for related resources fetches to eagerly load downstream resources being included in the response.
1 parent 87db448 commit 9e2a8c1

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

lib/jsonapi/relationship_builder.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def define_resource_relationship_accessor(type, relationship_name)
6060

6161
resource_klass = relationship.resource_klass
6262

63+
records = resource_klass.apply_includes(records, options)
64+
6365
filters = options.fetch(:filters, {})
6466
unless filters.nil? || filters.empty?
6567
records = resource_klass.apply_filters(records, filters, options)

test/controllers/controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3173,7 +3173,7 @@ def test_books_banned_non_book_admin_includes_nested_includes
31733173
end
31743174
assert_response :success
31753175
assert_equal 12, json_response['data'].size
3176-
assert_equal 132, json_response['included'].size
3176+
assert_equal 135, json_response['included'].size
31773177
assert_equal 'Book 0', json_response['data'][0]['attributes']['title']
31783178
assert_equal 901, json_response['meta']['record-count']
31793179
ensure

test/fixtures/book_comments.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
book_<%= book_num %>_comment_<%= comment_num %>:
55
id: <%= comment_id %>
66
body: This is comment <%= comment_num %> on book <%= book_num %>.
7-
author_id: <%= book_num.even? ? comment_id % 2 : (comment_id % 2) + 2 %>
7+
author_id: <%= (comment_id % 5) + 1 %>
88
book_id: <%= book_num %>
99
approved: <%= comment_num.even? %>
1010
<% comment_id = comment_id + 1 %>

test/integration/requests/request_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,30 @@ def test_pagination_empty_results
604604
# assert_equal 'This is comment 18 on book 1.', json_response['data'][9]['attributes']['body']
605605
# end
606606

607+
def test_query_count_related_resources
608+
# Expected Queries:
609+
# * Fetch specified book record
610+
# * Fetch book comment records associated with specified book
611+
# * Select count of book comment records for pagination
612+
assert_query_count 3 do
613+
get '/api/v2/books/1/book_comments?page[limit]=20'
614+
end
615+
assert_equal 20, json_response['data'].size
616+
end
617+
618+
def test_query_count_related_resources_with_includes
619+
# Expected Queries:
620+
# * Fetch specified book record
621+
# * Fetch book comment records associated with specified book
622+
# * Fetch all author records the book comments to be returned
623+
# * Select count of book comment records for pagination
624+
assert_query_count 4 do
625+
get '/api/v2/books/1/book_comments?page[limit]=20&include=author'
626+
end
627+
assert_equal 20, json_response['data'].size
628+
assert_equal 5, json_response['included'].size
629+
end
630+
607631

608632
def test_flow_self
609633
assert_cacheable_jsonapi_get '/posts/1'

0 commit comments

Comments
 (0)