hashicorp / hashicorp/terraform-plugin-sdk
TypeList has nil elements when required string attribute is set to empty string
- Dominant language
- Go
- Stars
- 485
- Forks
- 244
- Avg merge
- 19h 57m
- Merged PRs (30d)
- 4
Description
I'm working on a custom provider and I'm noticing some strange behavior with a `TypeList`, in which each element is a schema with a single required string attribute. If you set this attribute to an empty string, the element itself is `nil`, instead of a `map[string]interface{}`.
### Terraform Version
```
Terraform v0.12.11
```
### Terraform Configuration Files
Given a schema that looks like this:
```go
"test": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"a_string": {
Type: schema.TypeString,
Required: true,
},
},
},
},
```
And HCL that looks like this:
```hcl
resource "test_resource" "test" {
test {
a_string = ""
}
}
```
The following provider code produces a panic:
```go
if v, ok := data.GetOk("test"); ok {
test := v.([]interface{})[0].(map[string]interface{})
log.Printf("[DEBUG] test: %s", test["a_string"].(string))
}
```
As `v.([]interface{})[0]` is `nil`.
### Expected Behavior
In the example above, I expect `v.([]interface{})[0]` to be a `map[string]interface{}` with a single key, `a_string`, with the value of `""`
### Actual Behavior
In the example above, `v.([]interface{})[0]` was `nil`, causing the example code to panic.
### Additional Context
I'm a little confused with what the intended behavior is when the end user specifies an empty string for a string attribute that is marked as required. I actually expected the example HCL I posted to fail validation, since I interpreted a "required string" to also be non-empty.
If it is expected for users to be able to use an empty string for required string attributes, then what is the recommended way for a provider developer to ensure that required string attributes are also non-empty? I did a quick search in the `validation` package and didn't find a helper function that looked like it would help.
Thanks!
Contributor guide
Research direction
Start by reproducing the TypeList behavior with the nested schema and HCL configuration shown in the issue, then trace how the required empty string is represented in the provider data. Determine the intended handling for empty required strings and nested list elements; done means the behavior is covered by a regression test and the resulting provider-facing behavior matches the documented expectation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100