diff --git a/internal/configs/configschema/decoder_spec.go b/internal/configs/configschema/decoder_spec.go index e020895288..5fc6e77131 100644 --- a/internal/configs/configschema/decoder_spec.go +++ b/internal/configs/configschema/decoder_spec.go @@ -184,11 +184,11 @@ func (b *Block) DecoderSpec() hcldec.Spec { } func (a *Attribute) decoderSpec(name string) hcldec.Spec { - ret := &hcldec.AttrSpec{Name: name} - if a == nil { - return ret + if a == nil || (a.Type == cty.NilType && a.NestedType == nil) { + panic("Invalid attribute schema: schema is nil.") } + ret := &hcldec.AttrSpec{Name: name} if a.NestedType != nil { if a.Type != cty.NilType { panic("Invalid attribute schema: NestedType and Type cannot both be set. This is a bug in the provider.") diff --git a/internal/configs/configschema/decoder_spec_test.go b/internal/configs/configschema/decoder_spec_test.go index 111ce5fb9b..99d6e700d1 100644 --- a/internal/configs/configschema/decoder_spec_test.go +++ b/internal/configs/configschema/decoder_spec_test.go @@ -445,18 +445,6 @@ func TestAttributeDecoderSpec(t *testing.T) { Want cty.Value DiagCount int }{ - "empty": { - &Attribute{}, - hcl.EmptyBody(), - cty.NilVal, - 0, - }, - "nil": { - nil, - hcl.EmptyBody(), - cty.NilVal, - 0, - }, "optional string (null)": { &Attribute{ Type: cty.String, @@ -878,17 +866,33 @@ func TestAttributeDecoderSpec(t *testing.T) { // be removed when InternalValidate() is extended to validate Attribute specs // (and is used). See the #FIXME in decoderSpec. func TestAttributeDecoderSpec_panic(t *testing.T) { - attrS := &Attribute{ - Type: cty.Object(map[string]cty.Type{ - "nested_attribute": cty.String, - }), - NestedType: &Object{}, - Optional: true, + tests := map[string]struct { + Schema *Attribute + }{ + "empty": { + Schema: &Attribute{}, + }, + "nil": { + Schema: nil, + }, + "nested_attribute": { + Schema: &Attribute{ + Type: cty.Object(map[string]cty.Type{ + "nested_attribute": cty.String, + }), + NestedType: &Object{}, + Optional: true, + }, + }, } - defer func() { recover() }() - attrS.decoderSpec("attr") - t.Errorf("expected panic") + for name, test := range tests { + t.Run(name, func(t *testing.T) { + defer func() { recover() }() + test.Schema.decoderSpec("attr") + t.Errorf("expected panic") + }) + } } func TestListOptionalAttrsFromObject(t *testing.T) {