Skip to content

Commit cd11335

Browse files
committed
fix: Prevent saving a rule with an empty objectID
1 parent e3acc63 commit cd11335

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

algoliasearch/index.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,8 @@ def save_rule(self, rule, forward_to_replicas=False, request_options=None):
10591059
"""
10601060
if 'objectID' not in rule:
10611061
raise AlgoliaException('missing objectID in rule body')
1062+
if rule['objectID'] == '':
1063+
raise AlgoliaException('objectID in rule body cannot be empty')
10621064
params = {'forwardToReplicas': forward_to_replicas}
10631065
return self._req(False, '/rules/%s' % str(rule['objectID']), 'PUT', request_options, params, rule)
10641066

tests/test_index.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ def test_save_and_get_rule(index):
208208
assert index.read_rule('my-rule') == rule
209209

210210

211+
def test_empty_objectID_for_rule_should_raise_exception(index):
212+
rule = rule_stub()
213+
rule['objectID'] = ''
214+
215+
try:
216+
index.save_rule(rule)
217+
except AlgoliaException:
218+
return
219+
220+
# We should not be able to save a rule with an empty objectID.
221+
assert False
222+
211223
def test_delete_rule(index):
212224
rule = rule_stub()
213225
res = index.save_rule(rule)

0 commit comments

Comments
 (0)