Skip to content
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

Fix saving persisted model and delete item attributes with nil values #681

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ def delete(values)
# Replaces the values of one or more attributes
#
def set(values)
@updates.merge!(sanitize_attributes(values))
values_sanitized = sanitize_attributes(values)

if Dynamoid.config.store_attribute_with_nil_value
@updates.merge!(values_sanitized)
else
# delete explicitly attributes if assigned nil value and configured
# to not store nil values
values_to_update = values_sanitized.select { |_, v| !v.nil? }
values_to_delete = values_sanitized.select { |_, v| v.nil? }

@updates.merge!(values_to_update)
@deletions.merge!(values_to_delete)
end

end

#
Expand Down Expand Up @@ -85,6 +98,10 @@ def attribute_updates

private

# Keep in sync with AwsSdkV3.sanitize_item.
#
# The only difference is that to update item we need to track whether
# attribute value is nil or not.
def sanitize_attributes(attributes)
attributes.transform_values do |v|
if v.is_a?(Hash)
Expand Down
32 changes: 29 additions & 3 deletions spec/dynamoid/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2902,32 +2902,58 @@ def around_save_callback
end

context 'true', config: { store_attribute_with_nil_value: true } do
it 'keeps document attribute with nil' do
it 'keeps document attribute with nil when model is not persisted' do
obj = klass.new(age: nil)
obj.save

expect(raw_attributes(obj)).to include(age: nil)
end

it 'keeps document attribute with nil when model is persisted' do
obj = klass.create(age: 42)
obj.age = nil
obj.save

expect(raw_attributes(obj)).to include(age: nil)
end
end

context 'false', config: { store_attribute_with_nil_value: false } do
it 'does not keep document attribute with nil' do
it 'does not keep document attribute with nil when model is not persisted' do
obj = klass.new(age: nil)
obj.save

# doesn't contain :age key
expect(raw_attributes(obj).keys).to contain_exactly(:id, :created_at, :updated_at)
end

it 'does not keep document attribute with nil when model is persisted' do
obj = klass.create!(age: 42)
obj.age = nil
obj.save

# doesn't contain :age key
expect(raw_attributes(obj).keys).to contain_exactly(:id, :created_at, :updated_at)
end
end

context 'by default', config: { store_attribute_with_nil_value: nil } do
it 'does not keep document attribute with nil' do
it 'does not keep document attribute with nil when model is not persisted' do
obj = klass.new(age: nil)
obj.save

# doesn't contain :age key
expect(raw_attributes(obj).keys).to contain_exactly(:id, :created_at, :updated_at)
end

it 'does not keep document attribute with nil when model is persisted' do
obj = klass.create!(age: 42)
obj.age = nil
obj.save

# doesn't contain :age key
expect(raw_attributes(obj).keys).to contain_exactly(:id, :created_at, :updated_at)
end
end
end

Expand Down
Loading