command/jsonstate,plan: fix panic with null values (#23492)

The code responsible for marshalling attribute values was checking for
nil values, but not null.

Fixes #23485, #23274
This commit is contained in:
Kristin Laemmert 2019-11-25 15:01:38 -05:00 committed by GitHub
parent 211cf08b38
commit 99225b8d76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 2 deletions

View File

@ -26,7 +26,7 @@ type stateValues struct {
type attributeValues map[string]interface{}
func marshalAttributeValues(value cty.Value, schema *configschema.Block) attributeValues {
if value == cty.NilVal {
if value == cty.NilVal || value.IsNull() {
return nil
}
ret := make(attributeValues)

View File

@ -30,6 +30,18 @@ func TestMarshalAttributeValues(t *testing.T) {
},
nil,
},
{
cty.NullVal(cty.String),
&configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {
Type: cty.String,
Optional: true,
},
},
},
nil,
},
{
cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),

View File

@ -101,9 +101,10 @@ type resource struct {
type attributeValues map[string]interface{}
func marshalAttributeValues(value cty.Value, schema *configschema.Block) attributeValues {
if value == cty.NilVal {
if value == cty.NilVal || value.IsNull() {
return nil
}
ret := make(attributeValues)
it := value.ElementIterator()

View File

@ -91,6 +91,18 @@ func TestMarshalAttributeValues(t *testing.T) {
},
nil,
},
{
cty.NullVal(cty.String),
&configschema.Block{
Attributes: map[string]*configschema.Attribute{
"foo": {
Type: cty.String,
Optional: true,
},
},
},
nil,
},
{
cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),