Skip to content

Commit b59f093

Browse files
committed
add tests for not null refinement
1 parent 972929c commit b59f093

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

internal/fwserver/attribute_validation.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ func AttributeValidate(ctx context.Context, a fwschema.Attribute, req ValidateAt
140140
// Show deprecation warnings only for known values or unknown values with a "not null" refinement.
141141
if a.GetDeprecationMessage() != "" {
142142
if attributeConfig.IsUnknown() {
143-
// If the attr.Value supports checking for refinements, we should check if the eventual known value will be not null.
143+
// If the unknown value will eventually be not null, we return the deprecation message for the practitioner.
144144
val, ok := attributeConfig.(attr.ValueWithNotNullRefinement)
145145
if ok {
146146
if _, notNull := val.NotNullRefinement(); notNull {
147-
// If the unknown value will eventually be not null, we return the deprecation message for the practitioner.
148147
resp.Diagnostics.AddAttributeWarning(
149148
req.AttributePath,
150149
"Attribute Deprecated",

internal/fwserver/attribute_validation_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/google/go-cmp/cmp"
1313
"github.com/hashicorp/terraform-plugin-go/tftypes"
14+
tfrefinement "github.com/hashicorp/terraform-plugin-go/tftypes/refinement"
1415

1516
"github.com/hashicorp/terraform-plugin-framework/attr"
1617
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -490,6 +491,40 @@ func TestAttributeValidate(t *testing.T) {
490491
},
491492
resp: ValidateAttributeResponse{},
492493
},
494+
"deprecation-message-unknown-with-not-null-refinement": {
495+
req: ValidateAttributeRequest{
496+
AttributePath: path.Root("test"),
497+
Config: tfsdk.Config{
498+
Raw: tftypes.NewValue(tftypes.Object{
499+
AttributeTypes: map[string]tftypes.Type{
500+
"test": tftypes.String,
501+
},
502+
}, map[string]tftypes.Value{
503+
"test": tftypes.NewValue(tftypes.String, tftypes.UnknownValue).Refine(tfrefinement.Refinements{
504+
tfrefinement.KeyNullness: tfrefinement.NewNullness(false),
505+
}),
506+
}),
507+
Schema: testschema.Schema{
508+
Attributes: map[string]fwschema.Attribute{
509+
"test": testschema.Attribute{
510+
Type: types.StringType,
511+
Optional: true,
512+
DeprecationMessage: "Use something else instead.",
513+
},
514+
},
515+
},
516+
},
517+
},
518+
resp: ValidateAttributeResponse{
519+
Diagnostics: diag.Diagnostics{
520+
diag.NewAttributeWarningDiagnostic(
521+
path.Root("test"),
522+
"Attribute Deprecated",
523+
"Use something else instead.",
524+
),
525+
},
526+
},
527+
},
493528
"deprecation-message-dynamic-underlying-value-unknown": {
494529
req: ValidateAttributeRequest{
495530
AttributePath: path.Root("test"),

0 commit comments

Comments
 (0)