Skip to content

Commit

Permalink
command/jsonstate,plan: fix panic with null values (#23492)
Browse files Browse the repository at this point in the history
The code responsible for marshalling attribute values was checking for
nil values, but not null.

Fixes #23485, #23274
  • Loading branch information
mildwonkey authored Nov 25, 2019
1 parent 211cf08 commit 99225b8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion command/jsonplan/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions command/jsonplan/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion command/jsonstate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions command/jsonstate/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 99225b8

Please sign in to comment.