-
Notifications
You must be signed in to change notification settings - Fork 94
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
Skip validation of a values missing in config #259
Conversation
Similar crash reported in hashicorp/terraform-provider-awscc#382. terraform-plugin-framework/tfsdk/state.go Lines 80 to 100 in 5f18d80
|
@ewbankkit Thanks for pointing this out, provided failed test and fixed it as well. |
Hi @gzigzigzeo 👋 Thanks for submitting this and sorry you ran into trouble here. In general, the preference for this framework should be performing attribute validation in all cases. This allows provider developers to check against My recommendation here would be to revert the general validation logic and instead perform a if in == nil {
return diags
}
if !in.Type().Equal(tftypes.Number) { // ... Please reach out if you have any questions (although it may take a week for a next response). |
Hey, @bflad! Thank you for the clarification! In fact, reverting of this logic was my initial thought, and performing it in float64.go and int64.go fixed the issue. At that moment, I thought that it was a bit implicit and does not guarantee a custom user validations from failing. I now see why your approach is better and framework should not guarantee anything here. I am going to fix my implementation accordingly, add a counterpart tests for other built-in types and push it to this PR. Thanks again. |
@bflad Done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates, @gzigzigzeo. Since I didn't want to break your main
branch by updating it, this will get pulled in as a separate commit. I will also fix the Set
validation (#261) as part of pulling this in.
"Computed-Computed-object": { | ||
config: Config{ | ||
Raw: tftypes.NewValue(tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"name": tftypes.String, | ||
}, | ||
}, map[string]tftypes.Value{ | ||
"name": tftypes.NewValue(tftypes.String, "namevalue"), | ||
}), | ||
Schema: Schema{ | ||
Attributes: map[string]Attribute{ | ||
"name": { | ||
Type: testtypes.StringTypeWithValidateWarning{}, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
target: new(testtypes.String), | ||
expected: &testtypes.String{String: types.String{Value: "namevalue"}, CreatedBy: testtypes.StringTypeWithValidateWarning{}}, | ||
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("name"))}, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test looks identical to the one above, so will remove on merge.
@@ -91,11 +91,13 @@ func (s State) getAttributeValue(ctx context.Context, path *tftypes.AttributePat | |||
// If found, convert this value to an unknown value. | |||
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/186 | |||
|
|||
if attrTypeWithValidate, ok := attrType.(attr.TypeWithValidate); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to Config
, this logic should not be changed, will fix on merge.
@@ -1585,6 +1607,35 @@ func TestConfigGetAttributeValue(t *testing.T) { | |||
expected: testtypes.String{String: types.String{Value: "value"}, CreatedBy: testtypes.StringTypeWithValidateWarning{}}, | |||
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("test"))}, | |||
}, | |||
"AttrTypeInt64WithValidateError-nested-missing-in-config": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will ensure there is a covering test in plan as well.
Merged as c584343 |
This fork was created because of this PR hashicorp/terraform-plugin-framework#259 It was upstreamed as part of the 0.6.1 release: https://github.com/hashicorp/terraform-plugin-framework/releases/tag/v0.6.1 We can now remove the replace directive
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |
Problem
I have the following schema:
And the following config:
"spec"
is the base object,"options"
is the child computed object, and"max_sessions"
is the bottommost Int64 value, which, in turn, is also computed.options
might either be missing in my configuration at all, set partially or set completely, includingmax_sessions
attribute.When
options
section is missing in the configuration completely, Terraform crashes:Solution
That happens because Terraform Framework tries to validate the type of
spec.options.max_sessions
config value, which is missing in the config, hence, can't have a type to validate. It must be skipped.