Skip to content

Commit f6f2f4c

Browse files
authored
Merge pull request #19 from stevenharman/fail_loudly_when_casting_non_integer_ids_to_integers
Fail loudly when casting non-Integer ids
2 parents 9a76ea6 + e798bc6 commit f6f2f4c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/jsonapi_spec_helpers/helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def json_include(type, index = 0)
5454

5555
def json_ids(integers = false)
5656
ids = json['data'].map { |d| d['id'] }
57-
ids.map!(&:to_i) if integers
57+
ids.map! { |id| Integer(id) } if integers
5858
ids
5959
end
6060

spec/helpers_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,29 @@
150150
expect(json_ids(true)).to eq([1, 2])
151151
end
152152
end
153+
154+
context 'when ids are non-integers' do
155+
let(:index_json) do
156+
{
157+
'data' => [
158+
{
159+
'type' => 'posts',
160+
'id' => 'ABC123',
161+
'attributes' => { }
162+
},
163+
{
164+
'type' => 'posts',
165+
'id' => 'KTHXBBQ',
166+
'attributes' => { }
167+
}
168+
]
169+
}
170+
end
171+
172+
it 'fails loudly when trying to cast to integers' do
173+
expect{ json_ids(true) }.to raise_error ArgumentError
174+
end
175+
end
153176
end
154177

155178
describe '#json_included_types' do

0 commit comments

Comments
 (0)