diff --git a/lib/pact/shared/active_support_support.rb b/lib/pact/shared/active_support_support.rb index 40167d0..96c7bee 100644 --- a/lib/pact/shared/active_support_support.rb +++ b/lib/pact/shared/active_support_support.rb @@ -66,9 +66,9 @@ def warn_about_regexp(thing) private def fix_empty_hash_and_array json - json = json.gsub(/({\s*})/, "{\n }") - json.gsub(/\[\s*\]/, "[\n ]") json + .gsub(/({\s*})(?=,?$)/, "{\n }") + .gsub(/\[\s*\](?=,?$)/, "[\n ]") end end end diff --git a/spec/lib/pact/consumer_contract/active_support_support_spec.rb b/spec/lib/pact/consumer_contract/active_support_support_spec.rb index a441662..9ef8dba 100644 --- a/spec/lib/pact/consumer_contract/active_support_support_spec.rb +++ b/spec/lib/pact/consumer_contract/active_support_support_spec.rb @@ -77,6 +77,41 @@ def initialize it "pretty formats the json that has been not pretty formatted because of ActiveSupport" do expect(fix_json_formatting(active_support_affected_pretty_generated_json)).to eq (pretty_generated_json.strip) end + + context 'when the JSON includes empty arrays or hashes in a compact format' do + let(:active_support_affected_pretty_generated_json) do +'{ + "empty_hash": {}, + "empty_array": [] +}' + end + let(:pretty_generated_json) do +'{ + "empty_hash": { + }, + "empty_array": [ + ] +}' + end + + it "expands the empty hash/array to be multiline" do + expect(fix_json_formatting(active_support_affected_pretty_generated_json)).to eq(pretty_generated_json.strip) + end + end + + context 'when the JSON includes json-like strings inside' do + let(:pretty_generated_json) do +'{ + "not_really_an_empty_hash": "{}", + "not_really_an_empty_array": "[]" +}' + end + + it 'does not change the inner strings' do + expect(fix_json_formatting(pretty_generated_json)).to eq(pretty_generated_json) + end + + end end end -end \ No newline at end of file +end