Skip to content

Commit e128665

Browse files
jamisdanhealy
andauthored
MONGOID-5789 Allow nil attribute access (backport of #5836) (#5838)
* MONGOID-5789 database_field_name given nil or empty string should raise UnknownAttribute exception (#5836) * database_field_name given nil or empty string should raise UnknownAttribute exception * fix spec syntax * database_field_name return empty string instead of exception * `field` might be an empty string, not merely nil * fix specs to expect empty string instead of nil This is okay, because the database_field_name method is a private API. We can change the contract here without regard for who else might be using it. --------- Co-authored-by: Jamis Buck <jamis.buck@mongodb.com> * see if we can update the permissions for the Ruby directories --------- Co-authored-by: Dan Healy <dan@beyondludus.com>
1 parent 1c75d72 commit e128665

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ jobs:
197197
with:
198198
ruby-version: "${{matrix.ruby}}"
199199
bundler: 2
200+
- name: Change permissions
201+
run: chmod -R o-w /opt/hostedtoolcache/Ruby
200202
- name: bundle
201203
run: bundle install --jobs 4 --retry 3
202204
env:

lib/mongoid/fields.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ def traverse_association_tree(key, fields, associations, aliased_associations)
405405
#
406406
# @api private
407407
def database_field_name(name, relations, aliased_fields, aliased_associations)
408+
return '' unless name.present?
409+
408410
if Mongoid.broken_alias_handling
409-
return nil unless name
410411
normalized = name.to_s
411412
aliased_fields[normalized] || normalized
412413
else
413-
return nil unless name.present?
414414
key = name.to_s
415415
segment, remaining = key.split('.', 2)
416416

lib/mongoid/touchable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def touch(field = nil)
2525
current = Time.configured.now
2626
field = database_field_name(field)
2727
write_attribute(:updated_at, current) if respond_to?("updated_at=")
28-
write_attribute(field, current) if field
28+
write_attribute(field, current) if field.present?
2929

3030
# If the document being touched is embedded, touch its parents
3131
# all the way through the composition hierarchy to the root object,

spec/mongoid/attributes_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,22 @@
288288
end
289289
end
290290

291+
context "when given nil" do
292+
293+
it "returns nil" do
294+
expect(person[nil]).to be nil
295+
end
296+
297+
end
298+
299+
context "when given an empty string" do
300+
301+
it "returns nil" do
302+
expect(person[""]).to be nil
303+
end
304+
305+
end
306+
291307
context "when the field was not explicitly defined" do
292308

293309
context "when excluding with only and the field was not excluded" do

spec/mongoid/fields_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,12 +1845,12 @@ class DiscriminatorChild2 < DiscriminatorParent
18451845

18461846
context 'given nil' do
18471847
subject { Person.database_field_name(nil) }
1848-
it { is_expected.to eq nil }
1848+
it { is_expected.to eq '' }
18491849
end
18501850

18511851
context 'given an empty String' do
18521852
subject { Person.database_field_name('') }
1853-
it { is_expected.to eq nil }
1853+
it { is_expected.to eq '' }
18541854
end
18551855

18561856
context 'given a String' do
@@ -1869,7 +1869,7 @@ class DiscriminatorChild2 < DiscriminatorParent
18691869

18701870
context 'given nil' do
18711871
subject { Person.database_field_name(nil) }
1872-
it { is_expected.to eq nil }
1872+
it { is_expected.to eq '' }
18731873
end
18741874

18751875
context 'given an empty String' do

0 commit comments

Comments
 (0)