Skip to content

Commit 98cdec5

Browse files
Fix panic when gathering attribute (#3178)
We set the TF Dynamic type to nil so that we can handle it later: https://github.com/pulumi/pulumi-terraform-bridge/blob/main/pkg/tfgen/generate.go#L547. This means we have to ensure that we perform a nil check on the attribute being processed; we did so in one location but missed this one. This PR adds the missing check, plus a test to ensure the provider will not panic. Add missing nil check when gathering attributes Add test for panic Fixes pulumi/pulumi-terraform-provider#88.
1 parent fbdb0fe commit 98cdec5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pkg/tfgen/generate_schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ func (nt *schemaNestedTypes) gatherFromMember(member moduleMember) {
107107
nt.gatherFromProperties(p.Results(), member, member.name, member.rets, false)
108108
case *variable:
109109
contract.Assertf(member.config, `member.config`)
110+
if member.typ == nil {
111+
return
112+
}
110113
p := paths.NewProperyPath(paths.NewConfigPath(), member.propertyName)
111114
nt.gatherFromPropertyType(p, member, member.name, "", member.typ, false)
112115
}

pkg/tfgen/generate_schema_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ import (
3434
pschema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
3535
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
3636
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
37+
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
3738
"github.com/stretchr/testify/assert"
3839
"github.com/stretchr/testify/require"
3940

4041
bridgetesting "github.com/pulumi/pulumi-terraform-bridge/v3/internal/testing"
4142
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
4243
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
44+
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfgen/internal/paths"
4345
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfgen/internal/testprovider"
4446
sdkv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
4547
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/x/muxer"
@@ -725,6 +727,54 @@ func TestRegress1626(t *testing.T) {
725727
require.NoError(t, err)
726728
}
727729

730+
func Test_DynamicAttributeHandling(t *testing.T) {
731+
t.Parallel()
732+
733+
t.Run("should not panic when processing variable with dynamic attribute", func(t *testing.T) {
734+
dynamicVar := &variable{
735+
name: "dynamic_attr",
736+
config: true,
737+
propertyName: paths.PropertyName{Key: "dynamic_attr", Name: tokens.Name("dynamicAttr")},
738+
typ: nil, // This represents a dynamic attribute
739+
}
740+
741+
nt := &schemaNestedTypes{
742+
nameToType: make(map[string]*schemaNestedType),
743+
}
744+
assert.NotPanics(t, func() { nt.gatherFromMember(dynamicVar) })
745+
nt.gatherFromMember(dynamicVar)
746+
assert.Empty(t, nt.nameToType, "Dynamic attributes should not generate nested types")
747+
})
748+
749+
t.Run("should handle mixed variable types including dynamic", func(t *testing.T) {
750+
strVar := &variable{
751+
name: "string_attr",
752+
config: true,
753+
propertyName: paths.PropertyName{Key: "string_attr", Name: tokens.Name("stringAttr")},
754+
typ: &propertyType{
755+
kind: kindString,
756+
},
757+
}
758+
759+
dynamicVar := &variable{
760+
name: "dynamic_attr",
761+
config: true,
762+
propertyName: paths.PropertyName{Key: "dynamic_attr", Name: tokens.Name("dynamicAttr")},
763+
typ: nil, // Dynamic attribute
764+
}
765+
766+
// Create a module with both variables
767+
mod := &module{
768+
members: []moduleMember{strVar, dynamicVar},
769+
}
770+
771+
assert.NotPanics(t, func() { gatherSchemaNestedTypesForModule(mod) })
772+
result := gatherSchemaNestedTypesForModule(mod)
773+
assert.NotNil(t, result, "Result should not be nil")
774+
assert.Empty(t, result, "Dynamic attributes should not generate nested types")
775+
})
776+
}
777+
728778
func TestSinkHclDiagnostics(t *testing.T) {
729779
t.Parallel()
730780

0 commit comments

Comments
 (0)