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
This commit is contained in:
Simon Brady 2019-11-16 04:25:49 +13:00 committed by Kristin Laemmert
parent 9451f8ae74
commit 7a9fa93724
2 changed files with 21 additions and 1 deletions

View File

@ -502,7 +502,7 @@ func (p *blockBodyDiffPrinter) writeValue(val cty.Value, action plans.Action, in
ty, err := ctyjson.ImpliedType(src) ty, err := ctyjson.ImpliedType(src)
// check for the special case of "null", which decodes to nil, // check for the special case of "null", which decodes to nil,
// and just allow it to be printed out directly // 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) jv, err := ctyjson.Unmarshal(src, ty)
if err == nil { if err == nil {
p.buf.WriteString("jsonencode(") p.buf.WriteString("jsonencode(")

View File

@ -50,6 +50,26 @@ func TestResourceChange_primitiveTypes(t *testing.T) {
+ resource "test_instance" "example" { + resource "test_instance" "example" {
+ string = "null" + 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": { "deletion": {