Skip to content

Commit 010a14a

Browse files
committed
feat: use _id instead of id for the interaction identifier
1 parent 0d4cab9 commit 010a14a

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lib/pact_broker/pacts/content.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ def add_ids(interactions)
7979
interactions.map do | interaction |
8080
if interaction.is_a?(Hash)
8181
# just in case there is a previous ID in there
82-
interaction_without_id = interaction.reject { |k, _| k == "id" }
83-
interaction.merge("id" => generate_interaction_sha(interaction_without_id))
82+
interaction_without_id = interaction.reject { |k, _| k == "_id" }
83+
# make the _id the first key in the hash when rendered to JSON
84+
{ "_id" => generate_interaction_sha(interaction_without_id) }.merge(interaction)
8485
else
8586
interaction
8687
end

spec/lib/pact_broker/pacts/content_spec.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ module Pacts
2121

2222
subject { Content.from_hash(pact_hash).with_ids }
2323

24-
context "when the interaction has an existing id" do
25-
let(:interaction) { { "id" => "blah", "foo" => "bar" } }
24+
context "when the interaction has an existing _id" do
25+
let(:interaction) { { "_id" => "blah", "foo" => "bar" } }
2626

27-
it "removes the id before creating the sha" do
27+
it "removes the i_d before creating the sha" do
2828
expect(GenerateInteractionSha).to receive(:call).with("foo" => "bar")
2929
subject
3030
end
3131
end
3232

3333
context "when the interaction is a hash" do
34-
it "adds ids to the interactions" do
35-
expect(subject.interactions.first["id"]).to eq "some-id"
34+
it "adds an _id to each interaction" do
35+
expect(subject.interactions.first["_id"]).to eq "some-id"
3636
end
3737
end
3838

3939
context "when the interaction is not a hash" do
4040
let(:interaction) { 1 }
4141

42-
it "does not add an id" do
42+
it "does not add an _id" do
4343
expect(subject.interactions.first).to eq interaction
4444
end
4545
end
@@ -55,8 +55,8 @@ module Pacts
5555
}
5656
end
5757

58-
it "adds ids to the messages" do
59-
expect(subject.messages.first["id"]).to eq "some-id"
58+
it "adds an _id to each message" do
59+
expect(subject.messages.first["_id"]).to eq "some-id"
6060
end
6161
end
6262
end

0 commit comments

Comments
 (0)