Skip to content

Commit 50189f2

Browse files
committed
fix: prevent insertion of extra new lines in JSON
1 parent f6f1c50 commit 50189f2

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/pact/shared/active_support_support.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def warn_about_regexp(thing)
6666
private
6767

6868
def fix_empty_hash_and_array json
69-
json = json.gsub(/({\s*})/, "{\n }")
70-
json.gsub(/\[\s*\]/, "[\n ]")
7169
json
70+
.gsub(/({\s*})(?=,?$)/, "{\n }")
71+
.gsub(/\[\s*\](?=,?$)/, "[\n ]")
7272
end
7373
end
7474
end

spec/lib/pact/consumer_contract/active_support_support_spec.rb

+36-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,41 @@ def initialize
7777
it "pretty formats the json that has been not pretty formatted because of ActiveSupport" do
7878
expect(fix_json_formatting(active_support_affected_pretty_generated_json)).to eq (pretty_generated_json.strip)
7979
end
80+
81+
context 'when the JSON includes empty arrays or hashes in a compact format' do
82+
let(:active_support_affected_pretty_generated_json) do
83+
'{
84+
"empty_hash": {},
85+
"empty_array": []
86+
}'
87+
end
88+
let(:pretty_generated_json) do
89+
'{
90+
"empty_hash": {
91+
},
92+
"empty_array": [
93+
]
94+
}'
95+
end
96+
97+
it "expands the empty hash/array to be multiline" do
98+
expect(fix_json_formatting(active_support_affected_pretty_generated_json)).to eq(pretty_generated_json.strip)
99+
end
100+
end
101+
102+
context 'when the JSON includes json-like strings inside' do
103+
let(:pretty_generated_json) do
104+
'{
105+
"not_really_an_empty_hash": "{}",
106+
"not_really_an_empty_array": "[]"
107+
}'
108+
end
109+
110+
it 'does not change the inner strings' do
111+
expect(fix_json_formatting(pretty_generated_json)).to eq(pretty_generated_json)
112+
end
113+
114+
end
80115
end
81116
end
82-
end
117+
end

0 commit comments

Comments
 (0)