diff --git a/command/format/diff.go b/command/format/diff.go index 0f5ecab24b..b9d652722a 100644 --- a/command/format/diff.go +++ b/command/format/diff.go @@ -1067,7 +1067,7 @@ func ctySequenceDiff(old, new []cty.Value) []*plans.Change { var oldI, newI, lcsI int for oldI < len(old) || newI < len(new) || lcsI < len(lcs) { for oldI < len(old) && (lcsI >= len(lcs) || !old[oldI].RawEquals(lcs[lcsI])) { - isObjectDiff := old[oldI].Type().IsObjectType() && new[newI].Type().IsObjectType() + isObjectDiff := old[oldI].Type().IsObjectType() && (newI >= len(new) || new[newI].Type().IsObjectType()) if isObjectDiff && newI < len(new) { ret = append(ret, &plans.Change{ Action: plans.Update, diff --git a/command/format/diff_test.go b/command/format/diff_test.go index 7b86bdd21a..b266e6b0a8 100644 --- a/command/format/diff_test.go +++ b/command/format/diff_test.go @@ -761,7 +761,7 @@ func TestResourceChange_JSON(t *testing.T) { } `, }, - "JSON list of objects": { + "JSON list of objects - adding item": { Action: plans.Update, Mode: addrs.ManagedResourceMode, Before: cty.ObjectVal(map[string]cty.Value{ @@ -794,6 +794,41 @@ func TestResourceChange_JSON(t *testing.T) { ] ) } +`, + }, + "JSON list of objects - removing item": { + Action: plans.Update, + Mode: addrs.ManagedResourceMode, + Before: cty.ObjectVal(map[string]cty.Value{ + "id": cty.StringVal("i-02ae66f368e8518a9"), + "json_field": cty.StringVal(`[{"one": "111"}, {"two": "222"}]`), + }), + After: cty.ObjectVal(map[string]cty.Value{ + "id": cty.UnknownVal(cty.String), + "json_field": cty.StringVal(`[{"one": "111"}]`), + }), + Schema: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Optional: true, Computed: true}, + "json_field": {Type: cty.String, Optional: true}, + }, + }, + RequiredReplace: cty.NewPathSet(), + Tainted: false, + ExpectedOutput: ` # test_instance.example will be updated in-place + ~ resource "test_instance" "example" { + ~ id = "i-02ae66f368e8518a9" -> (known after apply) + ~ json_field = jsonencode( + ~ [ + { + one = "111" + }, + - { + - two = "222" + }, + ] + ) + } `, }, "JSON object with list of objects": {