Skip to content

Commit 562af0f

Browse files
committed
Maintain consistent JSON formatting
The JSON gem has historically included newlines when pretty printing empty arrays or hashes. This changed with ruby/json@b2c4480 in JSON 2.8.0. In order to maintain consistent behavior for our users, this commit special cases empty array and hash facts and adds a new test for empty hashes. (cherry picked from commit 295d2f9)
1 parent 5dcda19 commit 562af0f

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/puppet/face/facts.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,15 @@
164164

165165
case result
166166
when Array, Hash
167-
Puppet::Util::Json.dump(result, :pretty => true)
167+
# JSON < 2.8.0 would pretty print empty arrays and hashes with newlines
168+
# Maintain that behavior for our users for now
169+
if result.is_a?(Array) && result.empty?
170+
"[\n\n]"
171+
elsif result.is_a?(Hash) && result.empty?
172+
"{\n}"
173+
else
174+
Puppet::Util::Json.dump(result, :pretty => true)
175+
end
168176
else # one of VALID_TYPES above
169177
result
170178
end

spec/unit/application/facts_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191

9292
{
9393
"type_hash" => [{'a' => 2}, "{\n \"a\": 2\n}"],
94+
"type_empty_hash" => [{}, "{\n}"],
9495
"type_array" => [[], "[\n\n]"],
9596
"type_string" => ["str", "str"],
9697
"type_int" => [1, "1"],

0 commit comments

Comments
 (0)