Skip to content

Commit 73e0889

Browse files
committed
resource/schema: Ensure invalid attribute default value errors are raised
Reference: #590 Reference: #930 Previously the logic handling attribute `Default` values would silently ignore any type errors, which would lead to confusing planning data behaviors. This updates the logic to raise those error properly and adds covering unit testing. These error messages are using the underlying `tftypes` type system errors which is currently a pragmatic compromise throughout various parts of the framework logic that bridges between both type systems to save additional type assertion logic and potential bugs relating to those conversions. In the future if the internal `tftypes` handling and exported fields are replaced with the framework type system types, this logic would instead return error messaging based on the framework type system errors. This also will enhance the schema validation logic to check any `Default` response value and compare its type to the schema, which will raise framework type system errors during the `GetProviderSchema` RPC, or during schema unit testing if provider developers have implemented that additional testing.
1 parent f35653e commit 73e0889

20 files changed

+1655
-183
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'resource/schema: Ensured invalid attribute default value errors are raised'
3+
time: 2024-02-28T10:33:38.517635-05:00
4+
custom:
5+
Issue: "930"

internal/fwschema/diagnostics.go

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package fwschema
66
import (
77
"fmt"
88

9+
"github.com/hashicorp/terraform-plugin-framework/attr"
910
"github.com/hashicorp/terraform-plugin-framework/diag"
1011
"github.com/hashicorp/terraform-plugin-framework/path"
1112
)
@@ -43,3 +44,23 @@ func AttributeMissingElementTypeDiag(attributePath path.Path) diag.Diagnostic {
4344
"One of these fields is required to prevent other unexpected errors or panics.",
4445
)
4546
}
47+
48+
func AttributeDefaultElementTypeMismatchDiag(attributePath path.Path, expectedElementType attr.Type, actualElementType attr.Type) diag.Diagnostic {
49+
return diag.NewErrorDiagnostic(
50+
"Invalid Attribute Implementation",
51+
"When validating the schema, an implementation issue was found. "+
52+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
53+
fmt.Sprintf("%q has a default value of element type %q, but the schema expects a type of %q. ", attributePath, actualElementType, expectedElementType)+
54+
"The default value must match the type of the schema.",
55+
)
56+
}
57+
58+
func AttributeDefaultTypeMismatchDiag(attributePath path.Path, expectedType attr.Type, actualType attr.Type) diag.Diagnostic {
59+
return diag.NewErrorDiagnostic(
60+
"Invalid Attribute Implementation",
61+
"When validating the schema, an implementation issue was found. "+
62+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
63+
fmt.Sprintf("%q has a default value of type %q, but the schema expects a type of %q. ", attributePath, actualType, expectedType)+
64+
"The default value must match the type of the schema.",
65+
)
66+
}

internal/fwschemadata/data_default.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import (
2121
// when configRaw contains a null value at the same path.
2222
func (d *Data) TransformDefaults(ctx context.Context, configRaw tftypes.Value) diag.Diagnostics {
2323
var diags diag.Diagnostics
24+
var err error
2425

2526
configData := Data{
2627
Description: DataDescriptionConfiguration,
2728
Schema: d.Schema,
2829
TerraformValue: configRaw,
2930
}
3031

31-
// Errors are handled as richer diag.Diagnostics instead.
32-
d.TerraformValue, _ = tftypes.Transform(d.TerraformValue, func(tfTypePath *tftypes.AttributePath, tfTypeValue tftypes.Value) (tftypes.Value, error) {
32+
d.TerraformValue, err = tftypes.Transform(d.TerraformValue, func(tfTypePath *tftypes.AttributePath, tfTypeValue tftypes.Value) (tftypes.Value, error) {
3333
fwPath, fwPathDiags := fromtftypes.AttributePath(ctx, tfTypePath, d.Schema)
3434

3535
diags.Append(fwPathDiags...)
@@ -303,5 +303,15 @@ func (d *Data) TransformDefaults(ctx context.Context, configRaw tftypes.Value) d
303303
return tfTypeValue, nil
304304
})
305305

306+
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/930
307+
if err != nil {
308+
diags.Append(diag.NewErrorDiagnostic(
309+
"Error Handling Schema Defaults",
310+
"An unexpected error occurred while handling schema default values. "+
311+
"Please report the following to the provider developer:\n\n"+
312+
"Error: "+err.Error(),
313+
))
314+
}
315+
306316
return diags
307317
}

0 commit comments

Comments
 (0)