Skip to content

RUBY-3355 - Create when key '_id' (as string) is nil results in error #2808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/mongo/operation/shared/idable.rb
Original file line number Diff line number Diff line change
@@ -58,7 +58,13 @@ def has_id?(doc)
def ensure_ids(documents)
@ids = []
documents.collect do |doc|
doc_with_id = has_id?(doc) ? doc : doc.merge(_id: id_generator.generate)
doc_with_id = if has_id?(doc)
doc
else
doc.delete('_id')
doc.merge(_id: id_generator.generate)
end

@ids << id(doc_with_id)
doc_with_id
end
19 changes: 19 additions & 0 deletions spec/mongo/operation/insert_spec.rb
Original file line number Diff line number Diff line change
@@ -100,6 +100,25 @@
expect(inserted_ids).to eq(collection_ids)
end
end

context 'when document contains a nil id' do

let(:documents) do
[{ 'field' => 'test', '_id' => nil }]
end

let(:inserted_ids) do
insert.execute(authorized_primary, context: context).inserted_ids
end

let(:collection_ids) do
authorized_collection.find(field: 'test').collect { |d| d['_id'] }
end

it 'adds an id to the documents' do
expect(inserted_ids).to eq(collection_ids)
end
end
end

describe '#execute' do