Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/schema: Ensure invalid attribute default value errors are raised #933

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
austinvalle marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading
Loading