Skip to content

Commit

Permalink
Merge pull request #113 from dgsuarez/master
Browse files Browse the repository at this point in the history
Fix for generation of invalid json
  • Loading branch information
YOU54F authored Dec 5, 2024
2 parents f6f1c50 + 50189f2 commit 18e23f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/pact/shared/active_support_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 36 additions & 1 deletion spec/lib/pact/consumer_contract/active_support_support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end

0 comments on commit 18e23f0

Please sign in to comment.