hashicorp / hashicorp/terraform-plugin-framework

Errantly Reflecting a List Into a Struct Provides Incorrect Error Details

Open
#729 5 comments 1 reaction 0 assignees View on GitHub
bug reflection
Dominant language
Go
Stars
384
Forks
107
Avg merge
3m
Merged PRs (30d)
1

Description

### Module version

```
v1.2.0
```

### Relevant provider source code

The original issue occurred in a provider schema, although this is issue should theoretically be reproducible in data sources and resources as well.

Schema:

```go
schema.Schema{
Blocks: map[string]schema.Block{
"list_block": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{/* ... */},
},
},
},
}
```

Models:

```go
type ProviderModel struct {
ListBlock types.List `tfsdk:"list_block"`
}

type ListBlockModel struct {
// ...
}
```

Logic:

```go
func (p ExampleCloudProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data ProviderModel

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

if resp.Diagnostics.HasError() {
return
}

// this should be []ListBlockModel below
var listBlocks ListBlockModel

// omitting null/unknown checking for example brevity
// error is raised here if list has elements
resp.Diagnostics.Append(data.ListBlock.ElementsAs(ctx, &listBlocks, false)...)

if resp.Diagnostics.HasError() {
return
}

// ...
}
```

### Terraform Configuration Files

```hcl
list_block {
# ...
}
```

### Expected Behavior

The error message should indicate that it is necessary to reflect a list into a slice.

```
cannot reflect tftypes.List[tftypes.Object[...]] into a struct, must be a slice
```

### Actual Behavior

The error message indicates that it is necessary to reflect a list into an object.

```
Error: Value Conversion Error

{CONFIG SNIPPET}

An unexpected error was encountered trying to convert tftypes.Value into
provider.ProviderModel. This is always an error in the provider. Please
report the following to the provider developer:

cannot reflect tftypes.List[tftypes.Object[...]] into a struct, must be an object
```

It is likely this occurs with maps and sets as well, although I haven't personally verified.

### Steps to Reproduce

1. `go test -count=1 -v ./internal/provider` with a covering acceptance test

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.