From 99225b8d76c0df3faf063618b4600ce7aacec0eb Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Mon, 25 Nov 2019 15:01:38 -0500 Subject: [PATCH] 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 --- command/jsonplan/values.go | 2 +- command/jsonplan/values_test.go | 12 ++++++++++++ command/jsonstate/state.go | 3 ++- command/jsonstate/state_test.go | 12 ++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/command/jsonplan/values.go b/command/jsonplan/values.go index 498b0f6f7..caa8babf4 100644 --- a/command/jsonplan/values.go +++ b/command/jsonplan/values.go @@ -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) diff --git a/command/jsonplan/values_test.go b/command/jsonplan/values_test.go index 6b04dba02..8395ee0aa 100644 --- a/command/jsonplan/values_test.go +++ b/command/jsonplan/values_test.go @@ -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"), diff --git a/command/jsonstate/state.go b/command/jsonstate/state.go index 1923c6468..2fac8fbba 100644 --- a/command/jsonstate/state.go +++ b/command/jsonstate/state.go @@ -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() diff --git a/command/jsonstate/state_test.go b/command/jsonstate/state_test.go index f03dd2d45..e40c123c6 100644 --- a/command/jsonstate/state_test.go +++ b/command/jsonstate/state_test.go @@ -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"),