Skip to content

Commit 331dc8b

Browse files
committed
handle empty containers in ProposedNew NestedTypes
Empty containers of NestedTypes were not handled in ProposedNew, causing plans to be submitted with null values where there was configuration present.
1 parent 9029870 commit 331dc8b

2 files changed

Lines changed: 79 additions & 9 deletions

File tree

internal/plans/objchange/objchange.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ func proposedNewAttributes(attrs map[string]*configschema.Attribute, prior, conf
308308
}
309309

310310
func proposedNewNestedType(schema *configschema.Object, prior, config cty.Value) cty.Value {
311-
var newV cty.Value
311+
// If the config is null or empty, we will be using this default value.
312+
newV := config
313+
312314
switch schema.Nesting {
313315
case configschema.NestingSingle:
314316
if !config.IsNull() {
@@ -323,6 +325,7 @@ func proposedNewNestedType(schema *configschema.Object, prior, config cty.Value)
323325
if config.IsKnown() && !config.IsNull() {
324326
configVLen = config.LengthInt()
325327
}
328+
326329
if configVLen > 0 {
327330
newVals := make([]cty.Value, 0, configVLen)
328331
for it := config.ElementIterator(); it.Next(); {
@@ -345,8 +348,6 @@ func proposedNewNestedType(schema *configschema.Object, prior, config cty.Value)
345348
} else {
346349
newV = cty.ListVal(newVals)
347350
}
348-
} else {
349-
newV = cty.NullVal(schema.ImpliedType())
350351
}
351352

352353
case configschema.NestingMap:
@@ -378,8 +379,6 @@ func proposedNewNestedType(schema *configschema.Object, prior, config cty.Value)
378379
// object values so that elements might have different types
379380
// in case of dynamically-typed attributes.
380381
newV = cty.ObjectVal(newVals)
381-
} else {
382-
newV = cty.NullVal(schema.ImpliedType())
383382
}
384383
} else {
385384
configVLen := 0
@@ -403,8 +402,6 @@ func proposedNewNestedType(schema *configschema.Object, prior, config cty.Value)
403402
newVals[k] = cty.ObjectVal(newEV)
404403
}
405404
newV = cty.MapVal(newVals)
406-
} else {
407-
newV = cty.NullVal(schema.ImpliedType())
408405
}
409406
}
410407

@@ -446,8 +443,6 @@ func proposedNewNestedType(schema *configschema.Object, prior, config cty.Value)
446443
}
447444
}
448445
newV = cty.SetVal(newVals)
449-
} else {
450-
newV = cty.NullVal(schema.ImpliedType())
451446
}
452447
}
453448

internal/plans/objchange/objchange_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,81 @@ func TestProposedNew(t *testing.T) {
14611461
}))),
14621462
}),
14631463
},
1464+
"expected empty NestedTypes": {
1465+
&configschema.Block{
1466+
Attributes: map[string]*configschema.Attribute{
1467+
"set": {
1468+
NestedType: &configschema.Object{
1469+
Nesting: configschema.NestingSet,
1470+
Attributes: map[string]*configschema.Attribute{
1471+
"bar": {Type: cty.String},
1472+
},
1473+
},
1474+
Optional: true,
1475+
},
1476+
"map": {
1477+
NestedType: &configschema.Object{
1478+
Nesting: configschema.NestingMap,
1479+
Attributes: map[string]*configschema.Attribute{
1480+
"bar": {Type: cty.String},
1481+
},
1482+
},
1483+
Optional: true,
1484+
},
1485+
},
1486+
},
1487+
cty.ObjectVal(map[string]cty.Value{
1488+
"map": cty.MapValEmpty(cty.Object(map[string]cty.Type{"bar": cty.String})),
1489+
"set": cty.SetValEmpty(cty.Object(map[string]cty.Type{"bar": cty.String})),
1490+
}),
1491+
cty.ObjectVal(map[string]cty.Value{
1492+
"map": cty.MapValEmpty(cty.Object(map[string]cty.Type{"bar": cty.String})),
1493+
"set": cty.SetValEmpty(cty.Object(map[string]cty.Type{"bar": cty.String})),
1494+
}),
1495+
cty.ObjectVal(map[string]cty.Value{
1496+
"map": cty.MapValEmpty(cty.Object(map[string]cty.Type{"bar": cty.String})),
1497+
"set": cty.SetValEmpty(cty.Object(map[string]cty.Type{"bar": cty.String})),
1498+
}),
1499+
},
1500+
"optional types set replacement": {
1501+
&configschema.Block{
1502+
Attributes: map[string]*configschema.Attribute{
1503+
"set": {
1504+
NestedType: &configschema.Object{
1505+
Nesting: configschema.NestingSet,
1506+
Attributes: map[string]*configschema.Attribute{
1507+
"bar": {
1508+
Type: cty.String,
1509+
Required: true,
1510+
},
1511+
},
1512+
},
1513+
Optional: true,
1514+
},
1515+
},
1516+
},
1517+
cty.ObjectVal(map[string]cty.Value{
1518+
"set": cty.SetVal([]cty.Value{
1519+
cty.ObjectVal(map[string]cty.Value{
1520+
"bar": cty.StringVal("old"),
1521+
}),
1522+
}),
1523+
}),
1524+
cty.ObjectVal(map[string]cty.Value{
1525+
"set": cty.SetVal([]cty.Value{
1526+
cty.ObjectVal(map[string]cty.Value{
1527+
"bar": cty.StringVal("new"),
1528+
}),
1529+
}),
1530+
}),
1531+
cty.ObjectVal(map[string]cty.Value{
1532+
"set": cty.SetVal([]cty.Value{
1533+
cty.ObjectVal(map[string]cty.Value{
1534+
"bar": cty.StringVal("new"),
1535+
}),
1536+
}),
1537+
}),
1538+
},
14641539
}
14651540

14661541
for name, test := range tests {

0 commit comments

Comments
 (0)