Skip to content

Commit

Permalink
feat: ignore order of keys when generating interaction sha
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 25, 2019
1 parent 010a14a commit e6a0e8d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 35 deletions.
13 changes: 4 additions & 9 deletions lib/pact_broker/pacts/generate_interaction_sha.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
require 'digest/sha1'
require 'pact_broker/configuration'
require 'pact_broker/pacts/sort_content'
require 'pact_broker/pacts/parse'
require 'pact_broker/pacts/content'
require 'pact_broker/pacts/order_object'

module PactBroker
module Pacts
module GenerateInteractionSha
def self.call interaction_hash
ordered_interaction_hash = interaction_hash.keys.sort.each_with_object({}) do | key, new_interaction_hash |
new_interaction_hash[key] = interaction_hash[key]
end
extend OrderObject

Digest::SHA1.hexdigest(ordered_interaction_hash.to_json)
def self.call interaction_hash
Digest::SHA1.hexdigest(order_object(interaction_hash).to_json)
end

def generate_interaction_sha(interaction_hash)
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/pacts/order_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module PactBroker
module Pacts
class OrderObject
module OrderObject
def self.call thing
case thing
when Hash then order_hash(thing)
Expand All @@ -20,6 +20,10 @@ def self.order_hash hash
new_hash[key] = call(hash[key])
end
end

def order_object(thing)
OrderObject.call(thing)
end
end
end
end
29 changes: 4 additions & 25 deletions spec/lib/pact_broker/pacts/generate_interaction_sha_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,26 @@ module Pacts

let(:interaction_hash_with_different_key_order) do
{
"providerStates" => [
"name" => "bar",
"params" => {
"wiffle" => "bar",
"meep" => "eek"
}
],
"description" => "foo"
}
end

let(:interaction_hash_with_different_params_order) do
{
"description" => "foo",
"providerStates" => [
"name" => "bar",
"params" => {
"meep" => "eek",
"wiffle" => "bar"
}
]
],
"description" => "foo"
}
end

subject { GenerateInteractionSha.call(interaction_hash) }

it "generates a SHA based on the sorted keys" do
expect(subject).to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521"
expect(subject).to eq "57d06e151eca35083e4d6b585b4d4fab2e2ed6b7"
end

it "generates the same SHA if the top level keys are ordered differently" do
it "generates the same SHA if the keys are ordered differently" do
expect(subject).to eq GenerateInteractionSha.call(interaction_hash_with_different_key_order)
end

# This could be a whole lot smarter, but I'm not sure it's worth it.
# eg. order of provider state params doesn't matter, but the ordering
# of the provider states themselves may... who knows.
# Let's not try and be too smart about it until we have a use case to flesh it out.
it "generates a different SHA if any of the other keys are ordered differently" do
expect(subject).to_not eq GenerateInteractionSha.call(interaction_hash_with_different_params_order)
end
end
end
end
Expand Down

0 comments on commit e6a0e8d

Please sign in to comment.