Skip to content

Commit 4a21033

Browse files
author
Liam Cervante
committed
plan renderer: fix crash when updating a null attribute to unknown
1 parent a265b66 commit 4a21033

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

internal/command/jsonformat/computed/renderers/block.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (renderer blockRenderer) RenderHuman(diff computed.Diff, indent int, opts c
158158
sort.Strings(keys)
159159

160160
if renderer.blocks.UnknownBlocks[key] {
161-
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), plans.Create, false), "", opts)
161+
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), diff.Action, false), "", opts)
162162
}
163163

164164
for _, innerKey := range keys {
@@ -170,7 +170,7 @@ func (renderer blockRenderer) RenderHuman(diff computed.Diff, indent int, opts c
170170
setOpts.ForceForcesReplacement = diff.Replace
171171

172172
if renderer.blocks.UnknownBlocks[key] {
173-
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), plans.Create, false), "", opts)
173+
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), diff.Action, false), "", opts)
174174
}
175175

176176
for _, block := range renderer.blocks.SetBlocks[key] {
@@ -179,7 +179,7 @@ func (renderer blockRenderer) RenderHuman(diff computed.Diff, indent int, opts c
179179
case renderer.blocks.IsListBlock(key):
180180

181181
if renderer.blocks.UnknownBlocks[key] {
182-
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), plans.Create, false), "", opts)
182+
renderBlock(computed.NewDiff(Unknown(computed.Diff{}), diff.Action, false), "", opts)
183183
}
184184

185185
for _, block := range renderer.blocks.ListBlocks[key] {

internal/command/jsonformat/computed/renderers/renderer_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,13 @@ jsonencode(
433433
},
434434
expected: "0 -> (known after apply)",
435435
},
436+
"computed_update_from_null": {
437+
diff: computed.Diff{
438+
Renderer: Unknown(computed.Diff{}),
439+
Action: plans.Update,
440+
},
441+
expected: "(known after apply)",
442+
},
436443
"computed_create_forces_replacement": {
437444
diff: computed.Diff{
438445
Renderer: Unknown(computed.Diff{}),

internal/command/jsonformat/computed/renderers/unknown.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ type unknownRenderer struct {
2626
}
2727

2828
func (renderer unknownRenderer) RenderHuman(diff computed.Diff, indent int, opts computed.RenderHumanOpts) string {
29-
if diff.Action == plans.Create {
29+
30+
// the before renderer can be nil and not a create action when the provider
31+
// previously returned a null value for the computed attribute and is now
32+
// declaring they will recompute it as part of the next update.
33+
34+
if diff.Action == plans.Create || renderer.before.Renderer == nil {
3035
return fmt.Sprintf("(known after apply)%s", forcesReplacement(diff.Replace, opts))
3136
}
3237

internal/command/jsonformat/plan_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,7 +3548,7 @@ func TestResourceChange_nestedList(t *testing.T) {
35483548
id = "i-02ae66f368e8518a9"
35493549
# (1 unchanged attribute hidden)
35503550
3551-
+ root_block_device (known after apply)
3551+
~ root_block_device (known after apply)
35523552
- root_block_device {
35533553
- new_field = "new_value" -> null
35543554
- volume_type = "gp1" -> null
@@ -3607,7 +3607,7 @@ func TestResourceChange_nestedList(t *testing.T) {
36073607
id = "i-02ae66f368e8518a9"
36083608
# (1 unchanged attribute hidden)
36093609
3610-
+ root_block_device (known after apply)
3610+
~ root_block_device (known after apply)
36113611
- root_block_device {
36123612
- new_field = "new_value" -> null
36133613
- volume_type = "gp1" -> null # forces replacement
@@ -4275,7 +4275,7 @@ func TestResourceChange_nestedSet(t *testing.T) {
42754275
id = "i-02ae66f368e8518a9"
42764276
# (1 unchanged attribute hidden)
42774277
4278-
+ root_block_device (known after apply)
4278+
~ root_block_device (known after apply)
42794279
- root_block_device {
42804280
- new_field = "new_value" -> null
42814281
- volume_type = "gp1" -> null
@@ -4334,7 +4334,7 @@ func TestResourceChange_nestedSet(t *testing.T) {
43344334
id = "i-02ae66f368e8518a9"
43354335
# (1 unchanged attribute hidden)
43364336
4337-
+ root_block_device (known after apply)
4337+
~ root_block_device (known after apply)
43384338
- root_block_device {
43394339
- new_field = "new_value" -> null
43404340
- volume_type = "gp1" -> null # forces replacement
@@ -4805,7 +4805,7 @@ func TestResourceChange_nestedMap(t *testing.T) {
48054805
id = "i-02ae66f368e8518a9"
48064806
# (1 unchanged attribute hidden)
48074807
4808-
+ root_block_device (known after apply)
4808+
~ root_block_device (known after apply)
48094809
- root_block_device "gp1" {
48104810
- new_field = "new_value" -> null
48114811
- volume_type = "gp1" -> null
@@ -4864,7 +4864,7 @@ func TestResourceChange_nestedMap(t *testing.T) {
48644864
id = "i-02ae66f368e8518a9"
48654865
# (1 unchanged attribute hidden)
48664866
4867-
+ root_block_device (known after apply)
4867+
~ root_block_device (known after apply)
48684868
- root_block_device "gp1" {
48694869
- new_field = "new_value" -> null
48704870
- volume_type = "gp1" -> null # forces replacement

0 commit comments

Comments
 (0)