helper/schema: Tolerate incorrectly-specified collection elems

We historically tolerated this, so we need to tolerate it here too in
order to work correctly with existing provider code.
This commit is contained in:
Martin Atkins 2018-06-01 17:32:01 -07:00
parent 91def7b6f6
commit 3c10a3b213
2 changed files with 45 additions and 1 deletions

View File

@ -40,7 +40,7 @@ func (m schemaMap) CoreConfigSchema() *configschema.Block {
continue continue
} }
switch schema.Elem.(type) { switch schema.Elem.(type) {
case *Schema: case *Schema, ValueType:
ret.Attributes[name] = schema.coreConfigSchemaAttribute() ret.Attributes[name] = schema.coreConfigSchemaAttribute()
case *Resource: case *Resource:
ret.BlockTypes[name] = schema.coreConfigSchemaBlock() ret.BlockTypes[name] = schema.coreConfigSchemaBlock()
@ -118,6 +118,10 @@ func (s *Schema) coreConfigSchemaType() cty.Type {
switch set := s.Elem.(type) { switch set := s.Elem.(type) {
case *Schema: case *Schema:
elemType = set.coreConfigSchemaType() elemType = set.coreConfigSchemaType()
case ValueType:
// This represents a mistake in the provider code, but it's a
// common one so we'll just shim it.
elemType = (&Schema{Type: set}).coreConfigSchemaType()
case *Resource: case *Resource:
// In practice we don't actually use this for normal schema // In practice we don't actually use this for normal schema
// construction because we construct a NestedBlock in that // construction because we construct a NestedBlock in that

View File

@ -118,6 +118,46 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
BlockTypes: map[string]*configschema.NestedBlock{}, BlockTypes: map[string]*configschema.NestedBlock{},
}, },
}, },
"incorrectly-specified collections": {
// Historically we tolerated setting a type directly as the Elem
// attribute, rather than a Schema object. This is common enough
// in existing provider code that we must support it as an alias
// for a schema object with the given type.
map[string]*Schema{
"list": {
Type: TypeList,
Required: true,
Elem: TypeInt,
},
"set": {
Type: TypeSet,
Optional: true,
Elem: TypeString,
},
"map": {
Type: TypeMap,
Optional: true,
Elem: TypeBool,
},
},
&configschema.Block{
Attributes: map[string]*configschema.Attribute{
"list": {
Type: cty.List(cty.Number),
Required: true,
},
"set": {
Type: cty.Set(cty.String),
Optional: true,
},
"map": {
Type: cty.Map(cty.Bool),
Optional: true,
},
},
BlockTypes: map[string]*configschema.NestedBlock{},
},
},
"sub-resource collections": { "sub-resource collections": {
map[string]*Schema{ map[string]*Schema{
"list": { "list": {