Skip to content

Commit d693182

Browse files
committed
Raise MissingHashKey in non-transactional #find when a partition key is nil
1 parent b45229f commit d693182

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

lib/dynamoid/finders.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,27 @@ def find_by_id(id, options = {})
107107

108108
# @private
109109
def _find_all(ids, options = {})
110-
raise Errors::MissingRangeKey if range_key && ids.any? { |_pk, sk| sk.nil? }
111-
112110
ids = ids.map do |id|
113111
if range_key
114112
# expect [hash key, range key] pair
115113
pk, sk = id
114+
115+
if pk.nil?
116+
raise Errors::MissingHashKey
117+
end
118+
if sk.nil?
119+
raise Errors::MissingRangeKey
120+
end
121+
116122
pk_dumped = cast_and_dump(hash_key, pk)
117123
sk_dumped = cast_and_dump(range_key, sk)
118124

119125
[pk_dumped, sk_dumped]
120126
else
127+
if id.nil?
128+
raise Errors::MissingHashKey
129+
end
130+
121131
cast_and_dump(hash_key, id)
122132
end
123133
end
@@ -157,6 +167,7 @@ def _find_all(ids, options = {})
157167

158168
# @private
159169
def _find_by_id(id, options = {})
170+
raise Errors::MissingHashKey if id.nil?
160171
raise Errors::MissingRangeKey if range_key && options[:range_key].nil?
161172

162173
partition_key_dumped = cast_and_dump(hash_key, id)

spec/dynamoid/finders_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
klass.find('wrong-id')
2828
}.to raise_error(Dynamoid::Errors::RecordNotFound, "Couldn't find Document with primary key \"wrong-id\"")
2929
end
30+
31+
it 'raises MissingHashKey when partition key is nil' do
32+
klass.create_table
33+
expect {
34+
klass.find(nil)
35+
}.to raise_error(Dynamoid::Errors::MissingHashKey)
36+
end
3037
end
3138

3239
context 'composite primary key' do
@@ -42,6 +49,14 @@
4249
}.to raise_error(Dynamoid::Errors::RecordNotFound, "Couldn't find Cat with primary key (\"wrong-id\",100500)")
4350
end
4451

52+
it 'raises MissingHashKey when partition key is nil' do
53+
obj = klass_with_composite_key.create!(age: 12)
54+
55+
expect {
56+
klass_with_composite_key.find(nil, range_key: obj.age)
57+
}.to raise_error(Dynamoid::Errors::MissingHashKey)
58+
end
59+
4560
it 'raises MissingRangeKey when range key is not specified' do
4661
obj = klass_with_composite_key.create!(age: 12)
4762

@@ -171,6 +186,14 @@
171186
}.to raise_error(Dynamoid::Errors::RecordNotFound,
172187
"Couldn't find all Documents with primary keys [\"wrong-id\"] (found 0 results, but was looking for 1)")
173188
end
189+
190+
it 'raises MissingHashKey when any partition key is nil' do
191+
obj1, _ = (1..2).map { klass.create! }
192+
193+
expect {
194+
klass.find([obj1.id, nil])
195+
}.to raise_error(Dynamoid::Errors::MissingHashKey)
196+
end
174197
end
175198

176199
context 'composite primary key' do
@@ -214,9 +237,22 @@
214237
expect(klass_with_composite_key.find([obj1.id, obj1.age], [obj2.id, obj2.age])).to match_array(objects)
215238
end
216239

240+
it 'raises MissingHashKey when any partition key is nil' do
241+
objects = (1..2).map { |i| klass_with_composite_key.create!(age: i) }
242+
obj1, obj2 = objects
243+
244+
expect {
245+
klass_with_composite_key.find([[obj1.id, obj1.age], [nil, obj2.age]])
246+
}.to raise_error(Dynamoid::Errors::MissingHashKey)
247+
end
248+
217249
it 'raises MissingRangeKey when range key is not specified' do
218250
obj1, obj2 = klass_with_composite_key.create!([{ age: 1 }, { age: 2 }])
219251

252+
expect {
253+
klass_with_composite_key.find([[obj1.id, obj1.age], [obj2.id, nil]])
254+
}.to raise_error(Dynamoid::Errors::MissingRangeKey)
255+
220256
expect {
221257
klass_with_composite_key.find([obj1.id, obj2.id])
222258
}.to raise_error(Dynamoid::Errors::MissingRangeKey)

0 commit comments

Comments
 (0)