Merge pull request #21884 from hashicorp/jbardin/validate-null-diags

don't append nil elements to a diagnostic cty.Path
This commit is contained in:
James Bardin 2019-06-26 09:33:04 -04:00 committed by GitHub
commit 8e8eb6d6d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -1374,11 +1374,19 @@ func validateConfigNulls(v cty.Value, path cty.Path) []*proto.Diagnostic {
for it.Next() { for it.Next() {
kv, ev := it.Element() kv, ev := it.Element()
if ev.IsNull() { if ev.IsNull() {
// if this is a set, the kv is also going to be null which
// isn't a valid path element, so we can't append it to the
// diagnostic.
p := path
if !kv.IsNull() {
p = append(p, cty.IndexStep{Key: kv})
}
diags = append(diags, &proto.Diagnostic{ diags = append(diags, &proto.Diagnostic{
Severity: proto.Diagnostic_ERROR, Severity: proto.Diagnostic_ERROR,
Summary: "Null value found in list", Summary: "Null value found in list",
Detail: "Null values are not allowed for this attribute value.", Detail: "Null values are not allowed for this attribute value.",
Attribute: convert.PathToAttributePath(append(path, cty.IndexStep{Key: kv})), Attribute: convert.PathToAttributePath(p),
}) })
continue continue
} }

View File

@ -1352,6 +1352,17 @@ func TestValidateNulls(t *testing.T) {
}), }),
Err: true, Err: true,
}, },
{
Cfg: cty.ObjectVal(map[string]cty.Value{
"object": cty.ObjectVal(map[string]cty.Value{
"list": cty.SetVal([]cty.Value{
cty.StringVal("string"),
cty.NullVal(cty.String),
}),
}),
}),
Err: true,
},
} { } {
t.Run(strconv.Itoa(i), func(t *testing.T) { t.Run(strconv.Itoa(i), func(t *testing.T) {
d := validateConfigNulls(tc.Cfg, nil) d := validateConfigNulls(tc.Cfg, nil)