diff --git a/CHANGELOG.md b/CHANGELOG.md index a38db8e9b..a242046d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,10 @@ ##### Bug Fixes -* None. +* Standardize podspec JSON serialization to Ruby 2.7+ behavior. This may result in checksum changes for + projects using Ruby 2.6 (macOS System Ruby). + [Eric Amorde](https://github.com/amorde) + [#787](https://github.com/CocoaPods/Core/pull/787) ## 1.17.0 (2026-07-06) diff --git a/lib/cocoapods-core/specification/json.rb b/lib/cocoapods-core/specification/json.rb index f3524f48e..457c7b457 100644 --- a/lib/cocoapods-core/specification/json.rb +++ b/lib/cocoapods-core/specification/json.rb @@ -12,7 +12,12 @@ def to_json(*a) # def to_pretty_json(*a) require 'json' - JSON.pretty_generate(to_hash, *a) << "\n" + json = JSON.pretty_generate(to_hash, *a) + # json < 2.8 (the newest available on Ruby < 2.7) renders empty arrays + # as "[\n\n ]" and empty hashes as "{\n }", while newer versions render + # "[]" and "{}". Normalize to the compact form so the output, and the + # checksums derived from it, don't depend on the Ruby/json version. + json.gsub(/": \[\n\n\s*\]/, '": []').gsub(/": \{\n\s*\}/, '": {}') << "\n" end #-----------------------------------------------------------------------# diff --git a/spec/specification/json_spec.rb b/spec/specification/json_spec.rb index c75173b42..ae77b5183 100644 --- a/spec/specification/json_spec.rb +++ b/spec/specification/json_spec.rb @@ -112,17 +112,24 @@ module Pod end it 'maintains correct order of keys across versions' do - # json < 2.8.0 renders empty arrays and objects across multiple - # lines, newer versions render them compactly as [] / {}, so - # normalize empty collections before comparing. - normalize = ->(s) { s.gsub(/\[\s+\]/, '[]').gsub(/\{\s+\}/, '{}') } %w(14 15 16 17 18).each do |version| json = File.read(File.expand_path("../../fixtures/CannonPodder#{version}.podspec.json", __FILE__)) loaded_spec = Specification.from_json(json) new_json = loaded_spec.to_pretty_json - normalize.call(new_json).should.equal normalize.call(json) + new_json.should.equal json end end + + it 'renders empty collections compactly regardless of the json gem version' do + spec = Specification.new do |s| + s.name = 'Empty' + s.dependency 'Dep' + s.pod_target_xcconfig = {} + end + json = spec.to_pretty_json + json.should.include '"Dep": []' + json.should.include '"pod_target_xcconfig": {}' + end end #-------------------------------------------------------------------------#