diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 6773fe584..4c9ecd61a 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -427,6 +427,13 @@ func (m schemaMap) Diff( } } + // Remove any nil diffs just to keep things clean + for k, v := range result.Attributes { + if v == nil { + delete(result.Attributes, k) + } + } + // If this is a non-destroy diff, call any custom diff logic that has been // defined. if !result.DestroyTainted && customizeDiff != nil { @@ -521,13 +528,6 @@ func (m schemaMap) Diff( result = result2 } - // Remove any nil diffs just to keep things clean - for k, v := range result.Attributes { - if v == nil { - delete(result.Attributes, k) - } - } - // Go through and detect all of the ComputedWhens now that we've // finished the diff. // TODO diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index bda54ccee..7c4d20f0a 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -3142,6 +3142,53 @@ func TestSchemaMap_Diff(t *testing.T) { "var.foo": interfaceToVariableSwallowError(""), }, }, + + { + Name: "optional, computed, empty string should not crash in CustomizeDiff", + Schema: map[string]*Schema{ + "unrelated_set": { + Type: TypeSet, + Optional: true, + Elem: &Schema{Type: TypeString}, + }, + "stream_enabled": { + Type: TypeBool, + Optional: true, + }, + "stream_view_type": { + Type: TypeString, + Optional: true, + Computed: true, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "unrelated_set.#": "0", + "stream_enabled": "true", + "stream_view_type": "KEYS_ONLY", + }, + }, + Config: map[string]interface{}{ + "stream_enabled": false, + "stream_view_type": "", + }, + CustomizeDiff: func(diff *ResourceDiff, v interface{}) error { + v, ok := diff.GetOk("unrelated_set") + if ok { + return fmt.Errorf("Didn't expect unrelated_set: %#v", v) + } + return nil + }, + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "stream_enabled": { + Old: "true", + New: "false", + }, + }, + }, + }, } for i, tc := range cases {