diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 0a016bec32..e05acd395a 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -702,7 +702,6 @@ func (m schemaMap) diffSet( return nil } - oRaw := o if o == nil { o = &Set{F: schema.Set} } @@ -751,7 +750,7 @@ func (m schemaMap) diffSet( // If the counts are not the same, then record that diff changed := oldLen != newLen - if changed || all || oRaw == nil { + if changed || all { countSchema := &Schema{ Type: TypeInt, Computed: schema.Computed, @@ -812,7 +811,7 @@ func (m schemaMap) diffString( n = schema.StateFunc(n) } nraw := n - if nraw == nil { + if nraw == nil && o != nil { nraw = schema.Type.Zero() } if err := mapstructure.WeakDecode(o, &os); err != nil { diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index b282243ff9..b0d924f3ac 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -1967,7 +1967,7 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, - // #49 Set - Same as #47 but for sets + // #49 Set - Same as #48 but for sets { Schema: map[string]*Schema{ "route": &Schema{ @@ -2047,14 +2047,19 @@ func TestSchemaMap_Diff(t *testing.T) { }, }, - State: nil, + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "instances.#": "1", + "instances.3": "foo", + }, + }, Config: map[string]interface{}{}, Diff: &terraform.InstanceDiff{ Attributes: map[string]*terraform.ResourceAttrDiff{ "instances.#": &terraform.ResourceAttrDiff{ - Old: "0", + Old: "1", New: "0", RequiresNew: true, }, @@ -2095,6 +2100,46 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + + // #53 - Unset bool, not in state + { + Schema: map[string]*Schema{ + "force": &Schema{ + Type: TypeBool, + Optional: true, + ForceNew: true, + }, + }, + + State: nil, + + Config: map[string]interface{}{}, + + Diff: nil, + + Err: false, + }, + + // #54 - Unset set, not in state + { + Schema: map[string]*Schema{ + "metadata_keys": &Schema{ + Type: TypeSet, + Optional: true, + ForceNew: true, + Elem: &Schema{Type: TypeInt}, + Set: func(interface{}) int { return 0 }, + }, + }, + + State: nil, + + Config: map[string]interface{}{}, + + Diff: nil, + + Err: false, + }, } for i, tc := range cases {