Skip to content

Commit 5ffb5ae

Browse files
committed
Skip validation of a nested object values missing in config
1 parent 6278f5f commit 5ffb5ae

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

tfsdk/config.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ func (c Config) getAttributeValue(ctx context.Context, path *tftypes.AttributePa
9191
// If found, convert this value to an unknown value.
9292
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/186
9393

94-
if attrTypeWithValidate, ok := attrType.(attr.TypeWithValidate); ok {
95-
diags.Append(attrTypeWithValidate.Validate(ctx, tfValue, path)...)
94+
if err == nil {
95+
if attrTypeWithValidate, ok := attrType.(attr.TypeWithValidate); ok {
96+
diags.Append(attrTypeWithValidate.Validate(ctx, tfValue, path)...)
9697

97-
if diags.HasError() {
98-
return nil, diags
98+
if diags.HasError() {
99+
return nil, diags
100+
}
99101
}
100102
}
101103

tfsdk/config_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,28 @@ func TestConfigGetAttribute(t *testing.T) {
335335
expected: &testtypes.String{String: types.String{Value: "namevalue"}, CreatedBy: testtypes.StringTypeWithValidateWarning{}},
336336
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("name"))},
337337
},
338+
"Computed-Computed-object": {
339+
config: Config{
340+
Raw: tftypes.NewValue(tftypes.Object{
341+
AttributeTypes: map[string]tftypes.Type{
342+
"name": tftypes.String,
343+
},
344+
}, map[string]tftypes.Value{
345+
"name": tftypes.NewValue(tftypes.String, "namevalue"),
346+
}),
347+
Schema: Schema{
348+
Attributes: map[string]Attribute{
349+
"name": {
350+
Type: testtypes.StringTypeWithValidateWarning{},
351+
Required: true,
352+
},
353+
},
354+
},
355+
},
356+
target: new(testtypes.String),
357+
expected: &testtypes.String{String: types.String{Value: "namevalue"}, CreatedBy: testtypes.StringTypeWithValidateWarning{}},
358+
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("name"))},
359+
},
338360
}
339361

340362
for name, tc := range testCases {
@@ -1585,6 +1607,35 @@ func TestConfigGetAttributeValue(t *testing.T) {
15851607
expected: testtypes.String{String: types.String{Value: "value"}, CreatedBy: testtypes.StringTypeWithValidateWarning{}},
15861608
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("test"))},
15871609
},
1610+
"AttrTypeInt64WithValidateError-nested-missing-in-config": {
1611+
config: Config{
1612+
Raw: tftypes.NewValue(tftypes.Object{
1613+
AttributeTypes: map[string]tftypes.Type{
1614+
"parent": tftypes.Object{},
1615+
},
1616+
}, map[string]tftypes.Value{
1617+
"parent": tftypes.NewValue(tftypes.Object{}, nil),
1618+
}),
1619+
Schema: Schema{
1620+
Attributes: map[string]Attribute{
1621+
"parent": {
1622+
Attributes: SingleNestedAttributes(map[string]Attribute{
1623+
"test": {
1624+
Type: types.Int64Type,
1625+
Optional: true,
1626+
Computed: true,
1627+
},
1628+
}),
1629+
Computed: true,
1630+
Optional: true,
1631+
},
1632+
},
1633+
},
1634+
},
1635+
path: tftypes.NewAttributePath().WithAttributeName("parent").WithAttributeName("test"),
1636+
expected: types.Int64{Null: true},
1637+
expectedDiags: nil,
1638+
},
15881639
}
15891640

15901641
for name, tc := range testCases {

0 commit comments

Comments
 (0)