From b4a9ec7b378c64a63e96bfa236198b99bf2716df Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 28 Jun 2018 08:55:59 +1000 Subject: [PATCH] fix: serialize ArrayLike in query params without wrapping another array around it --- lib/pact/consumer_contract/query_hash.rb | 2 ++ spec/lib/pact/consumer_contract/query_hash_spec.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/pact/consumer_contract/query_hash.rb b/lib/pact/consumer_contract/query_hash.rb index 8395dda..7f5eb79 100644 --- a/lib/pact/consumer_contract/query_hash.rb +++ b/lib/pact/consumer_contract/query_hash.rb @@ -56,6 +56,8 @@ def convert_to_hash_of_arrays(query) def insert(hash, k, v) if Hash === v v.each {|k2, v2| insert(hash, :"#{k}[#{k2}]", v2) } + elsif Pact::ArrayLike === v + hash[k.to_sym] = v else hash[k.to_sym] = [*v] end diff --git a/spec/lib/pact/consumer_contract/query_hash_spec.rb b/spec/lib/pact/consumer_contract/query_hash_spec.rb index e2913aa..50dbcd2 100644 --- a/spec/lib/pact/consumer_contract/query_hash_spec.rb +++ b/spec/lib/pact/consumer_contract/query_hash_spec.rb @@ -82,6 +82,20 @@ module Pact end describe "#to_json" do + context "when the query contains an ArrayLike" do + let(:query) { { foo: Pact.each_like("1"), bar: "2" } } + let(:expected_json) do + { + foo: Pact.each_like("1"), + bar: ["2"] + }.to_json + end + + it "serialises the ArrayLike without wrapping an array around it" do + expect(subject.to_json).to eq expected_json + end + end + context "when the query contains a Pact::Term" do let(:term) { Pact::Term.new(generate: "thing", matcher: /th/) } let(:query) { { param: term } }