From 74dbb5008952947444338d0a1691870dd92d440b Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 19 Aug 2021 14:17:43 -0400 Subject: [PATCH 1/6] Don't recursively validate SingleNestedAttribute if it is Null. --- tfsdk/attribute.go | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/tfsdk/attribute.go b/tfsdk/attribute.go index d1d39c4f4..801d42325 100644 --- a/tfsdk/attribute.go +++ b/tfsdk/attribute.go @@ -341,18 +341,34 @@ func (a Attribute) validate(ctx context.Context, req ValidateAttributeRequest, r } } case NestingModeSingle: - for nestedName, nestedAttr := range a.Attributes.GetAttributes() { - nestedAttrReq := ValidateAttributeRequest{ - AttributePath: req.AttributePath.WithAttributeName(nestedName), - Config: req.Config, - } - nestedAttrResp := &ValidateAttributeResponse{ - Diagnostics: resp.Diagnostics, - } + o, ok := req.AttributeConfig.(types.Object) + + if !ok { + err := fmt.Errorf("unknown attribute value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath) + resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{ + Severity: tfprotov6.DiagnosticSeverityError, + Summary: "Attribute Validation Error", + Detail: "Attribute validation cannot walk schema. Report this to the provider developer:\n\n" + err.Error(), + Attribute: req.AttributePath, + }) - nestedAttr.validate(ctx, nestedAttrReq, nestedAttrResp) + return + } - resp.Diagnostics = nestedAttrResp.Diagnostics + if len(o.Attrs) > 0 { + for nestedName, nestedAttr := range a.Attributes.GetAttributes() { + nestedAttrReq := ValidateAttributeRequest{ + AttributePath: req.AttributePath.WithAttributeName(nestedName), + Config: req.Config, + } + nestedAttrResp := &ValidateAttributeResponse{ + Diagnostics: resp.Diagnostics, + } + + nestedAttr.validate(ctx, nestedAttrReq, nestedAttrResp) + + resp.Diagnostics = nestedAttrResp.Diagnostics + } } default: err := fmt.Errorf("unknown attribute validation nesting mode (%T: %v) at path: %s", nm, nm, req.AttributePath) From 51afa3ea4e660034f1d65d03f0e29a5f17727149 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 2 Sep 2021 07:45:08 -0400 Subject: [PATCH 2/6] Add CHANGELOG entry. --- .changelog/118.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/118.txt diff --git a/.changelog/118.txt b/.changelog/118.txt new file mode 100644 index 000000000..a76c4ffb1 --- /dev/null +++ b/.changelog/118.txt @@ -0,0 +1,3 @@ +```release-note:bug +tfsdk: Don't attempt validation on a Null-valued `SingleNestedAttribute` +``` \ No newline at end of file From 08b1b56e8686b6fc1baabf4f1fceda8dc79b3047 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 2 Sep 2021 14:15:43 -0400 Subject: [PATCH 3/6] Use 'o.Null' check. --- tfsdk/attribute.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfsdk/attribute.go b/tfsdk/attribute.go index 801d42325..12be4a168 100644 --- a/tfsdk/attribute.go +++ b/tfsdk/attribute.go @@ -355,7 +355,7 @@ func (a Attribute) validate(ctx context.Context, req ValidateAttributeRequest, r return } - if len(o.Attrs) > 0 { + if !o.Null { for nestedName, nestedAttr := range a.Attributes.GetAttributes() { nestedAttrReq := ValidateAttributeRequest{ AttributePath: req.AttributePath.WithAttributeName(nestedName), From 2205d8a30e3b109a8cc910e4324bf34730d64f5e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 2 Sep 2021 15:02:37 -0400 Subject: [PATCH 4/6] Also check for 'o.Unknown'. --- tfsdk/attribute.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfsdk/attribute.go b/tfsdk/attribute.go index 12be4a168..f6360ecc1 100644 --- a/tfsdk/attribute.go +++ b/tfsdk/attribute.go @@ -355,7 +355,7 @@ func (a Attribute) validate(ctx context.Context, req ValidateAttributeRequest, r return } - if !o.Null { + if !o.Null && !o.Unknown { for nestedName, nestedAttr := range a.Attributes.GetAttributes() { nestedAttrReq := ValidateAttributeRequest{ AttributePath: req.AttributePath.WithAttributeName(nestedName), From 1a88047e6126e33405980f7a2c8e86b1ebf7c2bc Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 3 Sep 2021 07:34:13 -0400 Subject: [PATCH 5/6] Update .changelog/118.txt Co-authored-by: Paddy --- .changelog/118.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/118.txt b/.changelog/118.txt index a76c4ffb1..b2c70c61d 100644 --- a/.changelog/118.txt +++ b/.changelog/118.txt @@ -1,3 +1,3 @@ ```release-note:bug -tfsdk: Don't attempt validation on a Null-valued `SingleNestedAttribute` +tfsdk: Don't attempt validation on the nested attributes of a null or unknown `SingleNestedAttribute` ``` \ No newline at end of file From c970be51acbd58f13138ea3f3f7d8cf2cb500735 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 8 Sep 2021 08:17:55 -0400 Subject: [PATCH 6/6] Use diag package helpers (#110). --- tfsdk/attribute.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tfsdk/attribute.go b/tfsdk/attribute.go index f6360ecc1..55c1770c4 100644 --- a/tfsdk/attribute.go +++ b/tfsdk/attribute.go @@ -345,12 +345,11 @@ func (a Attribute) validate(ctx context.Context, req ValidateAttributeRequest, r if !ok { err := fmt.Errorf("unknown attribute value type (%T) for nesting mode (%T) at path: %s", req.AttributeConfig, nm, req.AttributePath) - resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{ - Severity: tfprotov6.DiagnosticSeverityError, - Summary: "Attribute Validation Error", - Detail: "Attribute validation cannot walk schema. Report this to the provider developer:\n\n" + err.Error(), - Attribute: req.AttributePath, - }) + resp.Diagnostics.AddAttributeError( + req.AttributePath, + "Attribute Validation Error", + "Attribute validation cannot walk schema. Report this to the provider developer:\n\n"+err.Error(), + ) return }