Skip to content

Commit 0f0befc

Browse files
authored
Merge pull request Homebrew#16458 from Bo98/json-freeze
api: use freeze option for JSON parsing
2 parents 398d7db + 1c3c64a commit 0f0befc

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

Library/Homebrew/api.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def self.fetch(endpoint)
3030
end
3131
raise ArgumentError, "No file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success?
3232

33-
cache[endpoint] = JSON.parse(output.stdout)
33+
cache[endpoint] = JSON.parse(output.stdout, freeze: true)
3434
rescue JSON::ParserError
3535
raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}"
3636
end
@@ -96,7 +96,7 @@ def self.fetch_json_api_file(endpoint, target: HOMEBREW_CACHE_API/endpoint,
9696

9797
mtime = insecure_download ? Time.new(1970, 1, 1) : Time.now
9898
FileUtils.touch(target, mtime: mtime) unless skip_download
99-
JSON.parse(target.read)
99+
JSON.parse(target.read, freeze: true)
100100
rescue JSON::ParserError
101101
target.unlink
102102
retry_count += 1
@@ -170,7 +170,7 @@ def self.write_names_file(names, type, regenerate:)
170170
return false, "signature mismatch"
171171
end
172172

173-
[true, JSON.parse(json_data["payload"])]
173+
[true, JSON.parse(json_data["payload"], freeze: true)]
174174
end
175175

176176
sig { params(path: Pathname).returns(T.nilable(Tap)) }

Library/Homebrew/api/cask.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ def download_and_cache_data!
4747

4848
cache["renames"] = {}
4949
cache["casks"] = json_casks.to_h do |json_cask|
50-
token = json_cask.delete("token")
50+
token = json_cask["token"]
5151

5252
json_cask.fetch("old_tokens", []).each do |old_token|
5353
cache["renames"][old_token] = token
5454
end
5555

56-
[token, json_cask]
56+
[token, json_cask.except("token")]
5757
end
5858

5959
updated

Library/Homebrew/cask/cask.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def to_h
349349

350350
def to_hash_with_variations
351351
if loaded_from_api? && !Homebrew::EnvConfig.no_install_from_api?
352-
return api_to_local_hash(Homebrew::API::Cask.all_casks[token])
352+
return api_to_local_hash(Homebrew::API::Cask.all_casks[token].dup)
353353
end
354354

355355
hash = to_h

Library/Homebrew/formulary.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def self.load_formula_from_api(name, flags:)
232232
end
233233

234234
dep_json["uses_from_macos"]&.each_with_index do |dep, index|
235-
bounds = dep_json.fetch("uses_from_macos_bounds", [])[index] || {}
235+
bounds = dep_json.fetch("uses_from_macos_bounds", [])[index].dup || {}
236236
bounds.deep_transform_keys!(&:to_sym)
237237
bounds.deep_transform_values! { |val| val.is_a?(String) ? val.to_sym : val }
238238

0 commit comments

Comments
 (0)