Skip to content

Incorrect element type of default list or map value in schema causes all default computed values of schema to be silently set to null #930

@inhearst

Description

@inhearst

Module version

github.com/hashicorp/terraform-plugin-framework v1.5.0

Relevant provider source code

type ExampleResourceModel struct {
	ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
	Defaulted             types.String `tfsdk:"defaulted"`
	Id                    types.String `tfsdk:"id"`
	Foo                   []FooEntry   `tfsdk:"foo"`
}

type FooEntry struct {
}

func (r *ExampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
	resp.Schema = schema.Schema{
		// This description is used by the documentation generator and the language server.
		MarkdownDescription: "Example resource",

		Attributes: map[string]schema.Attribute{
			"configurable_attribute": schema.StringAttribute{
				MarkdownDescription: "Example configurable attribute",
				Optional:            true,
			},
			"defaulted": schema.StringAttribute{
				MarkdownDescription: "Example configurable attribute with default value",
				Optional:            true,
				Computed:            true,
				Default:             stringdefault.StaticString("example value when not configured"),
			},
			"id": schema.StringAttribute{
				Computed:            true,
				MarkdownDescription: "Example identifier",
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"foo": schema.ListNestedAttribute{
				MarkdownDescription: "test",
				Optional:            true,
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{},
				},
				Computed: true,
				Default:  listdefault.StaticValue(types.ListValueMust(types.Int64Type, []attr.Value{})),
			},
		},
	}
}

Terraform Configuration Files

terraform {
  required_providers {
    scaffolding = {
      source = "edu/scaffolding"
    }
  }
}

provider "scaffolding" {}

resource "scaffolding_example" "example" {
  configurable_attribute = "some-value"
}

output "test" {
  value = scaffolding_example.example
}

Expected Behavior

output.test should have defaulted set to "example value when not configured"

Actual Behavior

output.test.defaulted is null

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # scaffolding_example.example will be created
  + resource "scaffolding_example" "example" {
      + configurable_attribute = "some-value"
      + id                     = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + test = {
      + configurable_attribute = "some-value"
      + defaulted              = null
      + foo                    = null
      + id                     = (known after apply)
    }

When commenting out foo in the schema and the model, the correct values appear:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # scaffolding_example.example will be created
  + resource "scaffolding_example" "example" {
      + configurable_attribute = "some-value"
      + defaulted              = "example value when not configured"
      + id                     = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + test = {
      + configurable_attribute = "some-value"
      + defaulted              = "example value when not configured"
      + id                     = (known after apply)
    }

I am fairly new to Terraform plugins and golang, so I could be holding it wrong. But I think this case should at least raise an error. I spent many hours trying to figure out why my Optional+Computed properties were always null on plan/apply.

Steps to Reproduce

  1. Apply above patch to https://github.com/hashicorp/terraform-provider-scaffolding-framework
  2. go build
  3. cd examples/provider
  4. tf plan

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions