Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #545 from ripienaar/544
Browse files Browse the repository at this point in the history
(#544) Improve backward compatibility in JSON mode
  • Loading branch information
ripienaar committed Dec 2, 2018
2 parents 330553d + e12ea6c commit 85a3ce4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/mcollective/security/choria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "yaml"

require_relative "../util/choria"
require_relative "../util/indifferent_hash"

module MCollective
module Security
Expand Down Expand Up @@ -502,7 +503,7 @@ def deserialize(string, format=:json)
if format == :yaml
YAML.load(string)
else
JSON.parse(string)
JSON.parse(string, :object_class => Util::IndifferentHash)
end
end

Expand Down
12 changes: 12 additions & 0 deletions lib/mcollective/util/indifferent_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module MCollective
module Util
class IndifferentHash < Hash
def [](key)
return super if key?(key)
return self[key.to_s] if key.is_a?(Symbol)

super
end
end
end
end
4 changes: 3 additions & 1 deletion spec/unit/mcollective/security/choria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@ module MCollective
let(:d) { security.empty_request }

it "should default to json" do
expect(security.deserialize(d.to_json)).to eq(d)
result = security.deserialize(d.to_json)
expect(result).to eq(d)
expect(result).to be_a(Util::IndifferentHash)
end

it "should support yaml" do
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/mcollective/util/indifferent_hash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "spec_helper"
require "mcollective/util/indifferent_hash"

module MCollective
module Util
describe IndifferentHash do
describe "#[]" do
it "should correctly grant indifferent access" do
h = IndifferentHash["x" => "y"]
expect(h[:x]).to eq("y")
expect(h["x"]).to eq("y")

h = IndifferentHash[:x => "y"]
expect(h[:x]).to eq("y")
expect(h["x"]).to be_nil
end
end
end
end
end

0 comments on commit 85a3ce4

Please sign in to comment.