summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Hudson <jjhudson1@gmail.com>2016-02-26 17:42:23 -0700
committerJosh Hudson <jjhudson1@gmail.com>2016-02-26 17:42:23 -0700
commitd1e38f9586cc466e9b13a20da2c0fe0cf88a6184 (patch)
tree6eab40e232f073672ca1a3f8270f6b4f95f7b21b
parentf88ccdbea8a62d6488a77016a08a4a45b918f138 (diff)
parent6aa6c8945828deb50b2143ddf46f70697b9076fd (diff)
downloadchef-d1e38f9586cc466e9b13a20da2c0fe0cf88a6184.tar.gz
Merge pull request #4631 from chef/jjh/databag_item_to_hash_non_mutating
Fix a bug that was causing DataBagItem.to_hash to mutate the data bag item
-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