Skip to content

Commit 5b1336b

Browse files
committed
Merge pull request #273 from togglepro/no_record_count_in_show
Check if record_count is supported by request before adding to meta.
2 parents 17c14ba + 0857c9f commit 5b1336b

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

lib/jsonapi/response_document.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def top_level_meta
5050
@operation_results.results.each do |result|
5151
meta.merge!(result.meta)
5252

53-
if JSONAPI.configuration.top_level_meta_include_record_count
53+
if JSONAPI.configuration.top_level_meta_include_record_count && result.respond_to?(:record_count)
5454
meta[JSONAPI.configuration.top_level_meta_record_count_key] = result.record_count
5555
end
5656
end
@@ -123,4 +123,4 @@ def results_to_hash
123123
end
124124
end
125125
end
126-
end
126+
end

test/controllers/controller_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ def test_show_single
227227
assert_nil json_response['included']
228228
end
229229

230+
def test_show_does_not_include_records_count_in_meta
231+
JSONAPI.configuration.top_level_meta_include_record_count = true
232+
get :show, { id: Post.first.id }
233+
assert_response :success
234+
assert_equal json_response['meta'], nil
235+
ensure
236+
JSONAPI.configuration.top_level_meta_include_record_count = false
237+
end
238+
239+
230240
def test_show_single_with_includes
231241
get :show, {id: '1', include: 'comments'}
232242
assert_response :success
@@ -2397,4 +2407,4 @@ def test_save_model_callbacks_fail
23972407
assert_response :unprocessable_entity
23982408
assert_match /Save failed or was cancelled/, json_response['errors'][0]['detail']
23992409
end
2400-
end
2410+
end

0 commit comments

Comments
 (0)