Skip to content

Commit

Permalink
resource/schema: Ensure invalid attribute default value errors are ra…
Browse files Browse the repository at this point in the history
…ised (#933)

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.
  • Loading branch information
bflad committed Mar 1, 2024
1 parent f03ca33 commit bd22b58
Show file tree
Hide file tree
Showing 20 changed files with 1,887 additions and 183 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20240228-103338.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'resource/schema: Ensured invalid attribute default value errors are raised'
time: 2024-02-28T10:33:38.517635-05:00
custom:
Issue: "930"
21 changes: 21 additions & 0 deletions internal/fwschema/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fwschema
import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
)
Expand Down Expand Up @@ -43,3 +44,23 @@ func AttributeMissingElementTypeDiag(attributePath path.Path) diag.Diagnostic {
"One of these fields is required to prevent other unexpected errors or panics.",
)
}

func AttributeDefaultElementTypeMismatchDiag(attributePath path.Path, expectedElementType attr.Type, actualElementType attr.Type) diag.Diagnostic {
return diag.NewErrorDiagnostic(
"Invalid Attribute Implementation",
"When validating the schema, an implementation issue was found. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("%q has a default value of element type %q, but the schema expects a type of %q. ", attributePath, actualElementType, expectedElementType)+
"The default value must match the type of the schema.",
)
}

func AttributeDefaultTypeMismatchDiag(attributePath path.Path, expectedType attr.Type, actualType attr.Type) diag.Diagnostic {
return diag.NewErrorDiagnostic(
"Invalid Attribute Implementation",
"When validating the schema, an implementation issue was found. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
fmt.Sprintf("%q has a default value of type %q, but the schema expects a type of %q. ", attributePath, actualType, expectedType)+
"The default value must match the type of the schema.",
)
}
14 changes: 12 additions & 2 deletions internal/fwschemadata/data_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (
// when configRaw contains a null value at the same path.
func (d *Data) TransformDefaults(ctx context.Context, configRaw tftypes.Value) diag.Diagnostics {
var diags diag.Diagnostics
var err error

configData := Data{
Description: DataDescriptionConfiguration,
Schema: d.Schema,
TerraformValue: configRaw,
}

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

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

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

return diags
}

0 comments on commit bd22b58

Please sign in to comment.