From 7a9fa93724467502bac732f264a6acae1649c0a4 Mon Sep 17 00:00:00 2001 From: Simon Brady Date: Sat, 16 Nov 2019 04:25:49 +1300 Subject: [PATCH] command/plan: Fix panic in plan output with string containing null and whitespace (#23102) * command/plan: Fix panic in plan output with string containing null and whitespace * command/format: add test for null string with whitespace --- command/format/diff.go | 2 +- command/format/diff_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/command/format/diff.go b/command/format/diff.go index 18796ee5b..8f284a4a5 100644 --- a/command/format/diff.go +++ b/command/format/diff.go @@ -502,7 +502,7 @@ func (p *blockBodyDiffPrinter) writeValue(val cty.Value, action plans.Action, in ty, err := ctyjson.ImpliedType(src) // check for the special case of "null", which decodes to nil, // and just allow it to be printed out directly - if err == nil && !ty.IsPrimitiveType() && val.AsString() != "null" { + if err == nil && !ty.IsPrimitiveType() && strings.TrimSpace(val.AsString()) != "null" { jv, err := ctyjson.Unmarshal(src, ty) if err == nil { p.buf.WriteString("jsonencode(") diff --git a/command/format/diff_test.go b/command/format/diff_test.go index 991a62fba..96d414cb6 100644 --- a/command/format/diff_test.go +++ b/command/format/diff_test.go @@ -50,6 +50,26 @@ func TestResourceChange_primitiveTypes(t *testing.T) { + resource "test_instance" "example" { + string = "null" } +`, + }, + "creation (null string with extra whitespace)": { + Action: plans.Create, + Mode: addrs.ManagedResourceMode, + Before: cty.NullVal(cty.EmptyObject), + After: cty.ObjectVal(map[string]cty.Value{ + "string": cty.StringVal("null "), + }), + Schema: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "string": {Type: cty.String, Optional: true}, + }, + }, + RequiredReplace: cty.NewPathSet(), + Tainted: false, + ExpectedOutput: ` # test_instance.example will be created + + resource "test_instance" "example" { + + string = "null " + } `, }, "deletion": {