Skip to content

Commit

Permalink
Merge #557
Browse files Browse the repository at this point in the history
557: Add validation for document_id to prevent nil or empty values r=brunoocasali a=Trehana

# Pull Request

## Related issue
Fixes #556 

## What does this PR do?
Add validation for document_id to prevent nil or empty values

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Trehana Fernando <[email protected]>
  • Loading branch information
meili-bors[bot] and Trehana authored Sep 11, 2024
2 parents 3557ff5 + 57030fb commit 9d7c273
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-06-01 14:58:01 UTC using RuboCop version 1.63.5.
# on 2024-09-10 01:25:16 UTC using RuboCop version 1.65.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -15,7 +15,7 @@ Metrics/BlockLength:
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 430
Max: 433

# Offense count: 1
# Configuration parameters: Max, CountKeywordArgs.
Expand Down
9 changes: 9 additions & 0 deletions lib/meilisearch/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def initialize(message = nil)
end
end

class InvalidDocumentId < Error
attr_reader :message

def initialize(message = nil)
@message = "The document id is invalid. #{message}"
super(@message)
end
end

module TenantToken
class ExpireOrInvalidSignature < MeiliSearch::Error; end
class InvalidApiKey < MeiliSearch::Error; end
Expand Down
4 changes: 4 additions & 0 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ def delete_documents!(documents_ids)
alias delete_multiple_documents! delete_documents!

def delete_document(document_id)
if document_id.nil? || document_id.to_s.empty?
raise MeiliSearch::InvalidDocumentId, 'document_id cannot be empty or nil'
end

encode_document = URI.encode_www_form_component(document_id)
response = http_delete "/indexes/#{@uid}/documents/#{encode_document}"

Expand Down
8 changes: 8 additions & 0 deletions spec/meilisearch/client/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@
end.to raise_error(MeiliSearch::CommunicationError)
end
end

context 'when document id is invalid' do
it 'raises MeiliSearch::InvalidDocumentId' do
expect do
client.index('movies').delete_document(nil)
end.to raise_error(MeiliSearch::InvalidDocumentId)
end
end
end
4 changes: 4 additions & 0 deletions spec/meilisearch/index/documents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@
describe '#delete_document' do
before { index.add_documents(documents).await }

it 'if the document id is nil, it raises an error' do
expect { index.delete_document(nil) }.to raise_error(MeiliSearch::InvalidDocumentId)
end

it 'deletes one document from index' do
id = 456
index.delete_document(id).await
Expand Down

0 comments on commit 9d7c273

Please sign in to comment.