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:
parent
9451f8ae74
commit
7a9fa93724
|
@ -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(")
|
||||
|
|
|
@ -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": {
|
||||
|
|
Loading…
Reference in New Issue