Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json.pretty_generate behaviour changed in 2.8.0 #714

Closed
YOU54F opened this issue Nov 28, 2024 · 6 comments
Closed

json.pretty_generate behaviour changed in 2.8.0 #714

YOU54F opened this issue Nov 28, 2024 · 6 comments

Comments

@YOU54F
Copy link

YOU54F commented Nov 28, 2024

Issue: Empty hashes no longer print over two lines in json 2.8.0 when using JSON.pretty_generate

Expected: Empty hashes would print over two lines in json 2.8.0
Actual: Empty hashes would print on a single line in json 2.8.0

Observations: Only occurs in Ruby 3.0.x +, works correctly in 2.7

Reproducer

Change the gem version, and run script

# !/usr/bin/env ruby
if __FILE__ == $0

  require 'bundler/inline'

  gemfile(true) do
    source 'https://rubygems.org'

    gem 'json', '2.8.0'
    # gem 'json', '2.7.6'
  end

  expected_data_1 = {
    "thing": {
      "alligator": {
        "name": "Mary"
      }
    }
  }
  data_1 = { thing: { alligator: { name: 'Mary' } } }
  prettified_data_1 = JSON.pretty_generate(data_1)
  puts prettified_data_1


  data_2 = { thing: {} }
  prettified_data_2 = JSON.pretty_generate(data_2)
  puts prettified_data_2
end

Output

With Ruby 3.x

2.7.6

{
  "thing": {
    "alligator": {
      "name": "Mary"
    }
  }
}
{
  "thing": {
  }
}

2.8.0

{
  "thing": {
    "alligator": {
      "name": "Mary"
    }
  }
}
{
  "thing": {}
}
@YOU54F
Copy link
Author

YOU54F commented Nov 29, 2024

this change was originally requested to be optional and opt in.

#437

however it was implemented as the default with no way of preserving the existing behaviour

#626

also not noted as a breaking change in the release notes.

@byroot byroot closed this as completed in d0c38f2 Nov 29, 2024
@byroot
Copy link
Member

byroot commented Nov 29, 2024

Yes this change was fully intended I just forgot to include it in the changelog, sorry.

I don't really want to add an option for it as the new output is much more readable and there's already a tons of conditionals that hurt performance on this codepath.

Why is this change a problem for you?

@YOU54F
Copy link
Author

YOU54F commented Nov 29, 2024

thats okay sometimes things slip through the net.

its changed the output of our diffs for a project - see description in the following PR, which for us anyway isn’t correct.

pact-foundation/pact-support#111

@byroot
Copy link
Member

byroot commented Nov 29, 2024

Seems like you have a good workaround to make your test suite work with both versions.

@YOU54F
Copy link
Author

YOU54F commented Nov 29, 2024

thanks, I wasn’t sure if it was the right approach ( the workaround) or to raise an issue.

cheers for clarifying, will proceed with my PR now

@byroot
Copy link
Member

byroot commented Nov 29, 2024

I wasn’t sure if it was the right approach

Depends what you are trying to assert exactly. If the exact formatting doesn't matter, you can also compare JSON.parse results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants