Skip to content

Commit 295d2f9

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.
1 parent c3a5090 commit 295d2f9

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
@@ -167,7 +167,15 @@
167167

168168
case result
169169
when Array, Hash
170-
Puppet::Util::Json.dump(result, :pretty => true)
170+
# JSON < 2.8.0 would pretty print empty arrays and hashes with newlines
171+
# Maintain that behavior for our users for now
172+
if result.is_a?(Array) && result.empty?
173+
"[\n\n]"
174+
elsif result.is_a?(Hash) && result.empty?
175+
"{\n}"
176+
else
177+
Puppet::Util::Json.dump(result, :pretty => true)
178+
end
171179
else # one of VALID_TYPES above
172180
result
173181
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)