From 295d2f9071a46d82f73947963f761471bd011d88 Mon Sep 17 00:00:00 2001 From: Michael Hashizume Date: Mon, 25 Nov 2024 15:41:34 -0800 Subject: [PATCH] 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. --- lib/puppet/face/facts.rb | 10 +++++++++- spec/unit/application/facts_spec.rb | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/puppet/face/facts.rb b/lib/puppet/face/facts.rb index 2b117d0909c..2fbf30e989e 100644 --- a/lib/puppet/face/facts.rb +++ b/lib/puppet/face/facts.rb @@ -167,7 +167,15 @@ case result when Array, Hash - Puppet::Util::Json.dump(result, :pretty => true) + # JSON < 2.8.0 would pretty print empty arrays and hashes with newlines + # Maintain that behavior for our users for now + if result.is_a?(Array) && result.empty? + "[\n\n]" + elsif result.is_a?(Hash) && result.empty? + "{\n}" + else + Puppet::Util::Json.dump(result, :pretty => true) + end else # one of VALID_TYPES above result end diff --git a/spec/unit/application/facts_spec.rb b/spec/unit/application/facts_spec.rb index 24d08c59a20..c3aecd04257 100644 --- a/spec/unit/application/facts_spec.rb +++ b/spec/unit/application/facts_spec.rb @@ -91,6 +91,7 @@ { "type_hash" => [{'a' => 2}, "{\n \"a\": 2\n}"], + "type_empty_hash" => [{}, "{\n}"], "type_array" => [[], "[\n\n]"], "type_string" => ["str", "str"], "type_int" => [1, "1"],