Set state for optional nested attributes for SingleNestedAttributes

I am using terraform plugin framework v0.10.0 for provider development. For one of the attributes, I am using below schema.

“test_attribute”: {
Optional: true,
Computed: true,
Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
“flag1”: {
Optional: true,
Computed: true,
Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
“attribute1”: {
Type: types.BoolType,
Optional: true,
Computed: true,
PlanModifiers: tfsdk.AttributePlanModifiers{
DefaultAttribute(types.Bool{Value: false}),
},
},
“attribute2”: {
Type: types.BoolType,
Optional: true,
Computed: true,
PlanModifiers: tfsdk.AttributePlanModifiers{
DefaultAttribute(types.Bool{Value: false}),
},
},
}),
},
}),
},

In test_attribute, there are multiple flags. Each flag contains two attributes. Even if single flag is omitted, plugin framework is throwing below error.

An unexpected error was encountered trying to build a value. This is always an error in the provider. Please report the following to the provider developer:

│ unhandled unknown value

Hi @akashgs :wave: Thank you for raising this and sorry you are having issues with this.

We very recently merged changes that will release in version 0.14.0 of the framework (likely next week) which change this error message to try and be more helpful. The new error will look like:

Error: Value Conversion Error

An unexpected error was encountered trying to build a value. This is always an error in the provider. Please report the following to the provider developer:

Received unknown value, however the target type cannot handle unknown values. Use the corresponding `types` package type or a custom type that handles unknown values.

Path: ...
Target Type: ...
Suggested Type: ...

While you did not provide how you are accessing this schema-based data, my suspicion is that you might be trying to put the test_attribute data (or the underlying attributes) into normal Go types such as struct, *struct, bool, or *bool. Terraform supports three possibilities for configuration and plan values, either being a known value, null (missing), or unknown (known after apply). Normal Go types do not implement the Terraform concept for an unknown value, which is why the error is raised.

The best recommendation I can suggest without seeing more of the code involved is to try switching to the framework-defined types package value types, such as types.Object for the tfsdk.SingleNestedAttributes attributes and types.Bool for the types.BoolType attributes. These should prevent the error at the slight cost of being a little more difficult to descend into data structures. types.Object has an As() method to convert it to another Go type, if that is more convenient for your use case, or there are also helpers such as tfsdk.ValueAs() which accomplish something similar.

We are investigating right now whether we should always raise errors when types types (or custom types that support null/unknown values) are not used, instead of provider developers unfortunately making this discovery intermittently or worse after providers are released, since the situations in which unknown values are typically more practitioner configuration dependent and difficult to acceptance test. Consider Always Returning Errors with Get/GetAttribute for Target Types Without Unknown Support · Issue #498 · hashicorp/terraform-plugin-framework · GitHub

Hope this helps.