-
Notifications
You must be signed in to change notification settings - Fork 10.4k
genconfig: handle null nested attribute values without panic #38604
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
Open
net0pyr
wants to merge
1
commit into
hashicorp:main
Choose a base branch
from
net0pyr:fix/38569-null-nested-attr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| kind: BUG FIXES | ||
| body: '`terraform query -generate-config-out` no longer panics when the resource state contains a null nested-type attribute (for example an undeclared `timeouts` block).' | ||
| time: 2026-05-16T15:34:12+03:00 | ||
| custom: | ||
| Issue: "38569" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -426,6 +426,187 @@ resource "tfcoremock_simple_resource" "empty" { | |
| list = null | ||
| map = null | ||
| single = null | ||
| }`, | ||
| }, | ||
| // Regression for #38569: a fully-null resource state (which can | ||
| // reach genconfig from the list-resource flow when the provider | ||
| // returned no state for a queried resource) used to panic inside | ||
| // writeConfigNestedTypeAttributeFromExisting because cty.GetAttr | ||
| // is not safe to call on a null receiver. The expected output | ||
| // mirrors how the package already handles a non-null parent with | ||
| // null nested values (see "resource_with_nulls"). | ||
| "resource_with_null_state": { | ||
| schema: &configschema.Block{ | ||
| Attributes: map[string]*configschema.Attribute{ | ||
| "id": { | ||
| Type: cty.String, | ||
| Computed: true, | ||
| }, | ||
| "value": { | ||
| Type: cty.String, | ||
| Optional: true, | ||
| }, | ||
| "single": { | ||
| NestedType: &configschema.Object{ | ||
| Attributes: map[string]*configschema.Attribute{ | ||
| "nested_id": { | ||
| Type: cty.String, | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| Nesting: configschema.NestingSingle, | ||
| }, | ||
| Required: true, | ||
| }, | ||
| "list": { | ||
| NestedType: &configschema.Object{ | ||
| Attributes: map[string]*configschema.Attribute{ | ||
| "nested_id": { | ||
| Type: cty.String, | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| Nesting: configschema.NestingList, | ||
| }, | ||
| Required: true, | ||
| }, | ||
| "map": { | ||
| NestedType: &configschema.Object{ | ||
| Attributes: map[string]*configschema.Attribute{ | ||
| "nested_id": { | ||
| Type: cty.String, | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| Nesting: configschema.NestingMap, | ||
| }, | ||
| Required: true, | ||
| }, | ||
| }, | ||
| BlockTypes: map[string]*configschema.NestedBlock{ | ||
| "nested_single": { | ||
| Nesting: configschema.NestingSingle, | ||
| Block: configschema.Block{ | ||
| Attributes: map[string]*configschema.Attribute{ | ||
| "nested_id": { | ||
| Type: cty.String, | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| addr: addrs.AbsResourceInstance{ | ||
| Module: nil, | ||
| Resource: addrs.ResourceInstance{ | ||
| Resource: addrs.Resource{ | ||
| Mode: addrs.ManagedResourceMode, | ||
| Type: "tfcoremock_simple_resource", | ||
| Name: "empty", | ||
| }, | ||
| Key: nil, | ||
| }, | ||
| }, | ||
| provider: addrs.LocalProviderConfig{ | ||
| LocalName: "tfcoremock", | ||
| }, | ||
| value: cty.NullVal(cty.Object(map[string]cty.Type{ | ||
| "id": cty.String, | ||
| "value": cty.String, | ||
| "single": cty.Object(map[string]cty.Type{ | ||
| "nested_id": cty.String, | ||
| }), | ||
| "list": cty.List(cty.Object(map[string]cty.Type{ | ||
| "nested_id": cty.String, | ||
| })), | ||
| "map": cty.Map(cty.Object(map[string]cty.Type{ | ||
| "nested_id": cty.String, | ||
| })), | ||
| "nested_single": cty.Object(map[string]cty.Type{ | ||
| "nested_id": cty.String, | ||
| }), | ||
| })), | ||
| expected: ` | ||
| resource "tfcoremock_simple_resource" "empty" { | ||
| list = null | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like you're testing for null attributes, but the linked issue is about a null nested block for |
||
| map = null | ||
| single = null | ||
| value = null | ||
| }`, | ||
| }, | ||
| // Sibling to "resource_with_null_state": the parent is non-null | ||
| // and a nested attribute carries a real value. This guards | ||
| // against the #38569 null-parent guard becoming too eager and | ||
| // silently dropping real nested data. | ||
| "resource_with_nested_value_sibling": { | ||
| schema: &configschema.Block{ | ||
| Attributes: map[string]*configschema.Attribute{ | ||
| "id": { | ||
| Type: cty.String, | ||
| Computed: true, | ||
| }, | ||
| "single": { | ||
| NestedType: &configschema.Object{ | ||
| Attributes: map[string]*configschema.Attribute{ | ||
| "nested_id": { | ||
| Type: cty.String, | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| Nesting: configschema.NestingSingle, | ||
| }, | ||
| Required: true, | ||
| }, | ||
| "list": { | ||
| NestedType: &configschema.Object{ | ||
| Attributes: map[string]*configschema.Attribute{ | ||
| "nested_id": { | ||
| Type: cty.String, | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| Nesting: configschema.NestingList, | ||
| }, | ||
| Required: true, | ||
| }, | ||
| }, | ||
| }, | ||
| addr: addrs.AbsResourceInstance{ | ||
| Module: nil, | ||
| Resource: addrs.ResourceInstance{ | ||
| Resource: addrs.Resource{ | ||
| Mode: addrs.ManagedResourceMode, | ||
| Type: "tfcoremock_simple_resource", | ||
| Name: "empty", | ||
| }, | ||
| Key: nil, | ||
| }, | ||
| }, | ||
| provider: addrs.LocalProviderConfig{ | ||
| LocalName: "tfcoremock", | ||
| }, | ||
| value: cty.ObjectVal(map[string]cty.Value{ | ||
| "id": cty.StringVal("D2320658"), | ||
| "single": cty.ObjectVal(map[string]cty.Value{ | ||
| "nested_id": cty.StringVal("hello"), | ||
| }), | ||
| "list": cty.ListVal([]cty.Value{ | ||
| cty.ObjectVal(map[string]cty.Value{ | ||
| "nested_id": cty.StringVal("world"), | ||
| }), | ||
| }), | ||
| }), | ||
| expected: ` | ||
| resource "tfcoremock_simple_resource" "empty" { | ||
| list = [ | ||
| { | ||
| nested_id = "world" | ||
| }, | ||
| ] | ||
| single = { | ||
| nested_id = "hello" | ||
| } | ||
| }`, | ||
| }, | ||
| "simple_resource_with_stringified_json_object": { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
A null resource value means the resource instance does not exist, so it should not be returned from a list resource. You can't generate config for a null resource value, because it is not there to be confnigured.