Skip to content

Commit c81f712

Browse files
committed
Fix issue with nil @request when record is not found
1 parent bcd018c commit c81f712

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

lib/jsonapi/utils.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def jsonapi_render_bad_request
4242
end
4343

4444
def jsonapi_render_not_found
45+
setup_request
4546
id = extract_ids(@request.params)
4647
jsonapi_render_errors(JSONAPI::Exceptions::RecordNotFound.new(id))
4748
end

spec/support/shared/request_with_error.rb

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,30 @@
4141
end
4242
end
4343

44-
if options[:action] == :index
45-
context 'when error is at "filter" param' do
46-
options.merge!(
47-
params: { filter: { foo: 'bar' } },
48-
expect: { title: 'Filter not allowed', code: 102 }
49-
)
50-
it_behaves_like '400 response', options
51-
end
44+
context 'when error is at "filter" param' do
45+
options.merge!(
46+
params: { filter: { foo: 'bar' } },
47+
expect: { title: 'Filter not allowed', code: 102 }
48+
)
49+
it_behaves_like '400 response', options
50+
end
5251

53-
context 'when error is at "sort" param' do
54-
options.merge!(
55-
params: { sort: 'foo' },
56-
expect: { title: 'Invalid sort criteria', code: 114 }
57-
)
58-
it_behaves_like '400 response', options
59-
end
52+
context 'when error is at "sort" param' do
53+
options.merge!(
54+
params: { sort: 'foo' },
55+
expect: { title: 'Invalid sort criteria', code: 114 }
56+
)
57+
it_behaves_like '400 response', options
58+
end
6059

61-
context 'when error is at "page" param' do
62-
%i(number size).each do |field|
63-
context 'with invalid "number"' do
64-
options.merge!(
65-
params: { page: { "#{field}": 'foo' } },
66-
expect: { title: 'Invalid page value', code: 118 }
67-
)
68-
it_behaves_like '400 response', options
69-
end
70-
end
71-
end
72-
elsif !options[:record].nil?
73-
context 'with a not found record' do
74-
it 'renders 404 response' do
75-
get :show, options[:record]
76-
expect(response).to have_http_status :not_found
60+
context 'when error is at "page" param' do
61+
%i(number size).each do |field|
62+
context 'with invalid "number"' do
63+
options.merge!(
64+
params: { page: { "#{field}": 'foo' } },
65+
expect: { title: 'Invalid page value', code: 118 }
66+
)
67+
it_behaves_like '400 response', options
7768
end
7869
end
7970
end

spec/support/shared/rest/show.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
require 'spec_helper'
22

33
shared_examples_for 'JSON API #show action' do |options|
4+
context 'when invalid' do
5+
context 'when not found' do
6+
it 'renders 404 response' do
7+
options[:params] ||= {}
8+
get :show, options[:params].merge(id: 9999)
9+
expect(response).to have_http_status :not_found
10+
end
11+
end
12+
end
13+
414
context 'when valid' do
515
after(:each) { expect(response).to have_http_status :ok }
616

0 commit comments

Comments
 (0)