diff options
-rw-r--r-- | lib/chef/resource.rb | 11 | ||||
-rw-r--r-- | spec/unit/resource_spec.rb | 13 |
2 files changed, 21 insertions, 3 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 90453bd00e..3edcc24e17 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -664,13 +664,18 @@ class Chef end def to_hash + # Grab all current state, then any other ivars (backcompat) + result = {} + self.class.state_properties.each do |p| + result[p.name] = p.get(self) + end safe_ivars = instance_variables.map { |ivar| ivar.to_sym } - FORBIDDEN_IVARS - instance_vars = Hash.new safe_ivars.each do |iv| key = iv.to_s.sub(/^@/,'').to_sym - instance_vars[key] = instance_variable_get(iv) + next if result.has_key?(key) + result[key] = instance_variable_get(iv) end - instance_vars + result end def self.json_create(o) diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index b433a8062d..59951941d7 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -439,6 +439,19 @@ describe Chef::Resource do end describe "to_hash" do + context "when the resource has a property with a default" do + let(:resource_class) { Class.new(Chef::Resource) { property :a, default: 1 } } + it "should include the default in the hash" do + expect(resource.to_hash.keys.sort).to eq([:a, :allowed_actions, :params, :provider, :updated, + :updated_by_last_action, :before, :supports, + :noop, :ignore_failure, :name, :source_line, + :action, :retries, :retry_delay, :elapsed_time, + :default_guard_interpreter, :guard_interpreter, :sensitive].sort) + expect(resource.to_hash[:name]).to eq "funk" + expect(resource.to_hash[:a]).to eq 1 + end + end + it "should convert to a hash" do hash = resource.to_hash expected_keys = [ :allowed_actions, :params, :provider, :updated, |