hashicorp / hashicorp/terraform-plugin-framework

Defaults don't work in nested attributes

Open
#726 6 comments 0 reactions 0 assignees View on GitHub
bug documentation
Dominant language
Go
Stars
384
Forks
107
Avg merge
3m
Merged PRs (30d)
1

Description

### Module version
v1.2.0

### Relevant provider source code

```go
type TeamResourceTriageModel struct {
Enabled types.Bool `tfsdk:"enabled"`
}

type TeamResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Triage types.Object `tfsdk:"triage"`
}

func (r *TeamResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Linear team.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Identifier of the team.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the team.",
Required: true,
Validators: []validator.String{
stringvalidator.UTF8LengthAtLeast(2),
},
},
"triage": schema.SingleNestedAttribute{
MarkdownDescription: "Triage settings of the team.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
modifiers.UnknownAttributesOnUnknown(),
},
Attributes: map[string]schema.Attribute{
"enabled": schema.BoolAttribute{
MarkdownDescription: "Enable triage mode for the team. **Default** `false`.",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
},
},
},
},
}
}

func (r *TeamResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *TeamResourceModel
var triageData *TeamResourceTriageModel

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

input := TeamCreateInput{
Name: data.Name.ValueString(),
}

resp.Diagnostics.Append(data.Triage.As(ctx, &triageData, basetypes.ObjectAsOptions{})...)

if resp.Diagnostics.HasError() {
return
}

input.TriageEnabled = triageData.Enabled.ValueBool()

response, err := createTeam(ctx, *r.client, input)

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create team, got error: %s", err))
return
}

tflog.Trace(ctx, "created a team")

team := response.TeamCreate.Team

data.Id = types.StringValue(team.Id)

data.Triage = types.ObjectValueMust(
map[string]attr.Type{
"enabled": types.BoolType,
},
map[string]attr.Value{
"enabled": types.BoolValue(team.TriageEnabled),
},
)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
```

### Terraform Configuration Files

```hcl
resource "linear_team" "test" {
name = "test"
}
```

### Debug Output

### Expected Behavior

The Graphql Request sent to the server contains `triage_enabled` as `true` (read from the default)

### Actual Behavior

The Graphql Request sent to the server contains `triage_enabled` as `false`

### Steps to Reproduce

### References

I suspect this is caused by `modifiers.UnknownAttributesOnUnknown`, but it is recommended by @bflad at https://github.com/hashicorp/terraform-plugin-framework/issues/489#issuecomment-1255548450

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.