summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Hudson <jhudson@chef.io>2016-02-26 14:20:19 -0700
committerJosh Hudson <jhudson@chef.io>2016-02-26 14:20:19 -0700
commit6aa6c8945828deb50b2143ddf46f70697b9076fd (patch)
tree710ac573c7c9def92bb95c2433987ae7bcac2ecf
parent5f72ac57337bd4914d87a48bdfb49c2d1d039951 (diff)
downloadchef-jjh/databag_item_to_hash_non_mutating.tar.gz
Fix a bug that was causing DataBagItem.to_hash to mutate the data bag itemjjh/databag_item_to_hash_non_mutating
-rw-r--r--lib/chef/data_bag_item.rb4
-rw-r--r--spec/unit/data_bag_item_spec.rb7
2 files changed, 9 insertions, 2 deletions
diff --git a/lib/chef/data_bag_item.rb b/lib/chef/data_bag_item.rb
index 0e3fa48e68..6ac863a47d 100644
--- a/lib/chef/data_bag_item.rb
+++ b/lib/chef/data_bag_item.rb
@@ -107,9 +107,9 @@ class Chef
end
def to_hash
- result = self.raw_data
+ result = self.raw_data.dup
result["chef_type"] = "data_bag_item"
- result["data_bag"] = self.data_bag
+ result["data_bag"] = self.data_bag.to_s
result
end
diff --git a/spec/unit/data_bag_item_spec.rb b/spec/unit/data_bag_item_spec.rb
index ac4cf31b18..7f7d2b3013 100644
--- a/spec/unit/data_bag_item_spec.rb
+++ b/spec/unit/data_bag_item_spec.rb
@@ -146,6 +146,8 @@ describe Chef::DataBagItem do
data_bag_item
}
+ let!(:original_data_bag_keys) { data_bag_item.keys }
+
let(:to_hash) { data_bag_item.to_hash }
it "should return a hash" do
@@ -164,6 +166,11 @@ describe Chef::DataBagItem do
it "should have the data_bag set" do
expect(to_hash["data_bag"]).to eq("still_lost")
end
+
+ it "should not mutate the data_bag_item" do
+ data_bag_item.to_hash
+ expect(data_bag_item.keys).to eq(original_data_bag_keys)
+ end
end
describe "when deserializing from JSON" do