Skip to content

Commit

Permalink
MONGOID-5789 Allow nil attribute access (backport of #5836) (#5838)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* see if we can update the permissions for the Ruby directories

---------

Co-authored-by: Dan Healy <[email protected]>
  • Loading branch information
jamis and danhealy authored Jul 15, 2024
1 parent 1c75d72 commit e128665
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ jobs:
with:
ruby-version: "${{matrix.ruby}}"
bundler: 2
- name: Change permissions
run: chmod -R o-w /opt/hostedtoolcache/Ruby
- name: bundle
run: bundle install --jobs 4 --retry 3
env:
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,12 @@ def traverse_association_tree(key, fields, associations, aliased_associations)
#
# @api private
def database_field_name(name, relations, aliased_fields, aliased_associations)
return '' unless name.present?

if Mongoid.broken_alias_handling
return nil unless name
normalized = name.to_s
aliased_fields[normalized] || normalized
else
return nil unless name.present?
key = name.to_s
segment, remaining = key.split('.', 2)

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/touchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def touch(field = nil)
current = Time.configured.now
field = database_field_name(field)
write_attribute(:updated_at, current) if respond_to?("updated_at=")
write_attribute(field, current) if field
write_attribute(field, current) if field.present?

# If the document being touched is embedded, touch its parents
# all the way through the composition hierarchy to the root object,
Expand Down
16 changes: 16 additions & 0 deletions spec/mongoid/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,22 @@
end
end

context "when given nil" do

it "returns nil" do
expect(person[nil]).to be nil
end

end

context "when given an empty string" do

it "returns nil" do
expect(person[""]).to be nil
end

end

context "when the field was not explicitly defined" do

context "when excluding with only and the field was not excluded" do
Expand Down
6 changes: 3 additions & 3 deletions spec/mongoid/fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1845,12 +1845,12 @@ class DiscriminatorChild2 < DiscriminatorParent

context 'given nil' do
subject { Person.database_field_name(nil) }
it { is_expected.to eq nil }
it { is_expected.to eq '' }
end

context 'given an empty String' do
subject { Person.database_field_name('') }
it { is_expected.to eq nil }
it { is_expected.to eq '' }
end

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

context 'given nil' do
subject { Person.database_field_name(nil) }
it { is_expected.to eq nil }
it { is_expected.to eq '' }
end

context 'given an empty String' do
Expand Down

0 comments on commit e128665

Please sign in to comment.