Merge pull request #26028 from hashicorp/alisdair/fix-eval-read-data-panic

terraform: Fix createEmptyBlocks NestingSingle bug
This commit is contained in:
Alisdair McDiarmid 2020-08-28 10:29:28 -04:00 committed by GitHub
commit 8198e9758c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -239,8 +239,6 @@ func createEmptyBlocks(schema *configschema.Block, val cty.Value) cty.Value {
continue continue
} }
ety := block.Type().ElementType()
// helper to build the recursive block values // helper to build the recursive block values
nextBlocks := func() []cty.Value { nextBlocks := func() []cty.Value {
// this is only called once we know this is a non-null List or Set // this is only called once we know this is a non-null List or Set
@ -259,6 +257,7 @@ func createEmptyBlocks(schema *configschema.Block, val cty.Value) cty.Value {
// We are only concerned with block types that can come from the legacy // We are only concerned with block types that can come from the legacy
// sdk, which means TypeList or TypeSet. // sdk, which means TypeList or TypeSet.
case configschema.NestingList: case configschema.NestingList:
ety := block.Type().ElementType()
switch { switch {
case block.IsNull(): case block.IsNull():
objMap[name] = cty.ListValEmpty(ety) objMap[name] = cty.ListValEmpty(ety)
@ -269,6 +268,7 @@ func createEmptyBlocks(schema *configschema.Block, val cty.Value) cty.Value {
} }
case configschema.NestingSet: case configschema.NestingSet:
ety := block.Type().ElementType()
switch { switch {
case block.IsNull(): case block.IsNull():
objMap[name] = cty.SetValEmpty(ety) objMap[name] = cty.SetValEmpty(ety)

View File

@ -86,6 +86,22 @@ func TestReadDataCreateEmptyBlocks(t *testing.T) {
}, },
} }
singleSchema := &configschema.Block{
BlockTypes: map[string]*configschema.NestedBlock{
"single": {
Nesting: configschema.NestingSingle,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"attr": {
Type: cty.String,
Optional: true,
},
},
},
},
},
}
for _, tc := range []struct { for _, tc := range []struct {
name string name string
schema *configschema.Block schema *configschema.Block
@ -330,6 +346,20 @@ func TestReadDataCreateEmptyBlocks(t *testing.T) {
}), }),
}), }),
}, },
{
"single-block-null",
singleSchema,
cty.ObjectVal(map[string]cty.Value{
"single": cty.NullVal(cty.Object(map[string]cty.Type{
"attr": cty.String,
})),
}),
cty.ObjectVal(map[string]cty.Value{
"single": cty.NullVal(cty.Object(map[string]cty.Type{
"attr": cty.String,
})),
}),
},
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
val := createEmptyBlocks(tc.schema, tc.val) val := createEmptyBlocks(tc.schema, tc.val)