Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeList has nil elements when required string attribute is set to empty string #216

Open
mrparkers opened this issue Oct 28, 2019 · 0 comments
Labels
shims/maybe Issues that may be caused by our backwards-compatibility shims. subsystem/types Issues and feature requests related to the type system of Terraform and our shims around it. terraform-plugin-framework Resolved in terraform-plugin-framework

Comments

@mrparkers
Copy link

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:

"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:

resource "test_resource" "test" {
    test {
        a_string = ""
    }
}

The following provider code produces a panic:

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
shims/maybe Issues that may be caused by our backwards-compatibility shims. subsystem/types Issues and feature requests related to the type system of Terraform and our shims around it. terraform-plugin-framework Resolved in terraform-plugin-framework
Projects
None yet
Development

No branches or pull requests

3 participants