From 9dcbbaa642642a674dbdb65fc07979cafc6d3712 Mon Sep 17 00:00:00 2001 From: Trehana Fernando Date: Fri, 16 Aug 2024 17:13:42 +1000 Subject: [PATCH 1/5] #556 -Add validation for document_id to prevent nil or empty values --- lib/meilisearch/index.rb | 4 ++++ spec/meilisearch/index/documents_spec.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/meilisearch/index.rb b/lib/meilisearch/index.rb index 006c9c6c..05caaa78 100644 --- a/lib/meilisearch/index.rb +++ b/lib/meilisearch/index.rb @@ -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.empty? + raise MeiliSearch::Error::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}" diff --git a/spec/meilisearch/index/documents_spec.rb b/spec/meilisearch/index/documents_spec.rb index b1984f78..7238e21f 100644 --- a/spec/meilisearch/index/documents_spec.rb +++ b/spec/meilisearch/index/documents_spec.rb @@ -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).await }.to raise_error(MeiliSearch::ApiError) + end + it 'deletes one document from index' do id = 456 index.delete_document(id).await From 99f386d3c976bc2fb2624c36371bbcafd36dddb8 Mon Sep 17 00:00:00 2001 From: Trehana Fernando Date: Wed, 21 Aug 2024 10:47:56 +1000 Subject: [PATCH 2/5] Update index.rb --- lib/meilisearch/index.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/meilisearch/index.rb b/lib/meilisearch/index.rb index 05caaa78..7bc9d666 100644 --- a/lib/meilisearch/index.rb +++ b/lib/meilisearch/index.rb @@ -220,7 +220,7 @@ def delete_documents!(documents_ids) alias delete_multiple_documents! delete_documents! def delete_document(document_id) - if document_id.nil? || document_id.empty? + if document_id.nil? || document_id.to_s.empty? raise MeiliSearch::Error::InvalidDocumentId, 'document_id cannot be empty or nil' end From 6a9180328eae5263e313f9a57bfcae6a8f3e4ff9 Mon Sep 17 00:00:00 2001 From: Trehana Fernando Date: Wed, 21 Aug 2024 20:04:53 +1000 Subject: [PATCH 3/5] Update documents_spec.rb --- spec/meilisearch/index/documents_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/meilisearch/index/documents_spec.rb b/spec/meilisearch/index/documents_spec.rb index 7238e21f..16fbce81 100644 --- a/spec/meilisearch/index/documents_spec.rb +++ b/spec/meilisearch/index/documents_spec.rb @@ -394,7 +394,7 @@ before { index.add_documents(documents).await } it 'if the document id is nil, it raises an error' do - expect { index.delete_document(nil).await }.to raise_error(MeiliSearch::ApiError) + expect { index.delete_document(nil) }.to raise_error(MeiliSearch::Error::InvalidDocumentId) end it 'deletes one document from index' do From 68a9dedd9d6dc38151fa4bc76a671c00f4d9ede2 Mon Sep 17 00:00:00 2001 From: Trehana Fernando Date: Wed, 28 Aug 2024 19:41:57 +1000 Subject: [PATCH 4/5] Adding an InvalidDocument Error Class --- lib/meilisearch/error.rb | 9 +++++++++ lib/meilisearch/index.rb | 2 +- spec/meilisearch/client/errors_spec.rb | 8 ++++++++ spec/meilisearch/index/documents_spec.rb | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/meilisearch/error.rb b/lib/meilisearch/error.rb index 01420f09..02a1f40e 100644 --- a/lib/meilisearch/error.rb +++ b/lib/meilisearch/error.rb @@ -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 diff --git a/lib/meilisearch/index.rb b/lib/meilisearch/index.rb index 7bc9d666..0f6b7300 100644 --- a/lib/meilisearch/index.rb +++ b/lib/meilisearch/index.rb @@ -221,7 +221,7 @@ def delete_documents!(documents_ids) def delete_document(document_id) if document_id.nil? || document_id.to_s.empty? - raise MeiliSearch::Error::InvalidDocumentId, 'document_id cannot be empty or nil' + raise MeiliSearch::InvalidDocumentId, 'document_id cannot be empty or nil' end encode_document = URI.encode_www_form_component(document_id) diff --git a/spec/meilisearch/client/errors_spec.rb b/spec/meilisearch/client/errors_spec.rb index 10843cf4..1c010c68 100644 --- a/spec/meilisearch/client/errors_spec.rb +++ b/spec/meilisearch/client/errors_spec.rb @@ -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 diff --git a/spec/meilisearch/index/documents_spec.rb b/spec/meilisearch/index/documents_spec.rb index 16fbce81..d54d5a5f 100644 --- a/spec/meilisearch/index/documents_spec.rb +++ b/spec/meilisearch/index/documents_spec.rb @@ -394,7 +394,7 @@ 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::Error::InvalidDocumentId) + expect { index.delete_document(nil) }.to raise_error(MeiliSearch::InvalidDocumentId) end it 'deletes one document from index' do From 57030fbf75422da3aefaf67eaf80bfa94f8db02c Mon Sep 17 00:00:00 2001 From: Trehana Fernando Date: Tue, 10 Sep 2024 11:25:46 +1000 Subject: [PATCH 5/5] Update .rubocop_todo.yml --- .rubocop_todo.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7e767927..d106574a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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.