Skip to content

Commit

Permalink
Merge pull request #111 from pact-foundation/fix/diff_change_ruby_3_j…
Browse files Browse the repository at this point in the history
…son_2_8_regression

fix: add blank lines to empty hashs - json 2.8.x regression
  • Loading branch information
YOU54F authored Nov 29, 2024
2 parents df97a7d + 3c2ae45 commit 855d495
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pact/matchers/unix_diff_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def generate_string diff, target
begin
# Can't think of an elegant way to check if we can pretty generate other than to try it and maybe fail
json = fix_blank_lines_in_empty_hashes JSON.pretty_generate(comparable)
json = add_blank_lines_in_empty_hashes json
json = add_blank_lines_in_empty_arrays json
add_comma_to_end_of_arrays json
rescue JSON::GeneratorError
comparable.to_s
Expand Down
9 changes: 9 additions & 0 deletions lib/pact/shared/jruby_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@ def fix_blank_lines_in_empty_hashes json
json.gsub(/({\n)\n(\s*})/,'\1\2')
end

# preserve pre json 2.8.x behaviour
# https://github.com/ruby/json/pull/626
def add_blank_lines_in_empty_hashes(json)
json.gsub(/({\s*})/, "{\n }")
end

def add_blank_lines_in_empty_arrays(json)
json.gsub(/\[\s*\]/, "[\n ]")
end
end
end
1 change: 1 addition & 0 deletions spec/lib/pact/matchers/unix_diff_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ module Matchers
end

it "generates the right number of lines, even with ActiveSupport loaded" do
puts subject
expect(line_count).to eq 7 + key_lines_count
end
end
Expand Down

0 comments on commit 855d495

Please sign in to comment.