Skip to content

Commit e6a0e8d

Browse files
committed
feat: ignore order of keys when generating interaction sha
1 parent 010a14a commit e6a0e8d

File tree

3 files changed

+13
-35
lines changed

3 files changed

+13
-35
lines changed

lib/pact_broker/pacts/generate_interaction_sha.rb

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
require 'digest/sha1'
2-
require 'pact_broker/configuration'
3-
require 'pact_broker/pacts/sort_content'
4-
require 'pact_broker/pacts/parse'
5-
require 'pact_broker/pacts/content'
2+
require 'pact_broker/pacts/order_object'
63

74
module PactBroker
85
module Pacts
96
module GenerateInteractionSha
10-
def self.call interaction_hash
11-
ordered_interaction_hash = interaction_hash.keys.sort.each_with_object({}) do | key, new_interaction_hash |
12-
new_interaction_hash[key] = interaction_hash[key]
13-
end
7+
extend OrderObject
148

15-
Digest::SHA1.hexdigest(ordered_interaction_hash.to_json)
9+
def self.call interaction_hash
10+
Digest::SHA1.hexdigest(order_object(interaction_hash).to_json)
1611
end
1712

1813
def generate_interaction_sha(interaction_hash)

lib/pact_broker/pacts/order_object.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module PactBroker
44
module Pacts
5-
class OrderObject
5+
module OrderObject
66
def self.call thing
77
case thing
88
when Hash then order_hash(thing)
@@ -20,6 +20,10 @@ def self.order_hash hash
2020
new_hash[key] = call(hash[key])
2121
end
2222
end
23+
24+
def order_object(thing)
25+
OrderObject.call(thing)
26+
end
2327
end
2428
end
2529
end

spec/lib/pact_broker/pacts/generate_interaction_sha_spec.rb

+4-25
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,26 @@ module Pacts
1919

2020
let(:interaction_hash_with_different_key_order) do
2121
{
22-
"providerStates" => [
23-
"name" => "bar",
24-
"params" => {
25-
"wiffle" => "bar",
26-
"meep" => "eek"
27-
}
28-
],
29-
"description" => "foo"
30-
}
31-
end
32-
33-
let(:interaction_hash_with_different_params_order) do
34-
{
35-
"description" => "foo",
3622
"providerStates" => [
3723
"name" => "bar",
3824
"params" => {
3925
"meep" => "eek",
4026
"wiffle" => "bar"
4127
}
42-
]
28+
],
29+
"description" => "foo"
4330
}
4431
end
4532

4633
subject { GenerateInteractionSha.call(interaction_hash) }
4734

4835
it "generates a SHA based on the sorted keys" do
49-
expect(subject).to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521"
36+
expect(subject).to eq "57d06e151eca35083e4d6b585b4d4fab2e2ed6b7"
5037
end
5138

52-
it "generates the same SHA if the top level keys are ordered differently" do
39+
it "generates the same SHA if the keys are ordered differently" do
5340
expect(subject).to eq GenerateInteractionSha.call(interaction_hash_with_different_key_order)
5441
end
55-
56-
# This could be a whole lot smarter, but I'm not sure it's worth it.
57-
# eg. order of provider state params doesn't matter, but the ordering
58-
# of the provider states themselves may... who knows.
59-
# Let's not try and be too smart about it until we have a use case to flesh it out.
60-
it "generates a different SHA if any of the other keys are ordered differently" do
61-
expect(subject).to_not eq GenerateInteractionSha.call(interaction_hash_with_different_params_order)
62-
end
6342
end
6443
end
6544
end

0 commit comments

Comments
 (0)