Skip to content

CustomType defined on NestedObject within List/Map/Set Nested Attributes Raises Invalid Element Type Error #821

@bendbennett

Description

@bendbennett

Module version

v1.3.4

Relevant provider source code

var _ basetypes.ObjectTypable

type ListNestedAttributeAssocExtType struct {
	basetypes.ObjectType
}

func (c ListNestedAttributeAssocExtType) Equal(o attr.Type) bool {
	other, ok := o.(ListNestedAttributeAssocExtType)

	if !ok {
		return false
	}

	return c.ObjectType.Equal(other.ObjectType)
}

func (c ListNestedAttributeAssocExtType) String() string {
	return "CustomObjectType"
}

func (c ListNestedAttributeAssocExtType) ValueFromObject(ctx context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
	value := ListNestedAttributeAssocExtValue{
		ObjectValue: in,
	}

	return value, nil
}

func (c ListNestedAttributeAssocExtType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
	attrValue, err := c.ObjectType.ValueFromTerraform(ctx, in)

	if err != nil {
		return nil, err
	}

	objectValue, ok := attrValue.(basetypes.ObjectValue)

	if !ok {
		return nil, fmt.Errorf("unexpected value type of %T", attrValue)
	}

	objectValuable, diags := c.ValueFromObject(ctx, objectValue)

	if diags.HasError() {
		return nil, fmt.Errorf("unexpected error converting ObjectValue to ObjectValuable: %v", diags)
	}

	return objectValuable, nil
}

func (c ListNestedAttributeAssocExtType) ValueType(ctx context.Context) attr.Value {
	return ListNestedAttributeAssocExtValue{}
}

var _ basetypes.ObjectValuable = ListNestedAttributeAssocExtValue{}

type ListNestedAttributeAssocExtValue struct {
	basetypes.ObjectValue
}

func (c ListNestedAttributeAssocExtValue) Equal(o attr.Value) bool {
	other, ok := o.(ListNestedAttributeAssocExtValue)

	if !ok {
		return false
	}

	return c.ObjectValue.Equal(other.ObjectValue)
}

func (c ListNestedAttributeAssocExtValue) Type(ctx context.Context) attr.Type {
	return ListNestedAttributeAssocExtType{
		basetypes.ObjectType{
			AttrTypes: map[string]attr.Type{
				"int64_attribute": types.Int64Type,
			},
		},
	}
}

func (e *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"list_nested_attribute_assoc_ext_type": schema.ListNestedAttribute{
				Optional: true,
				NestedObject: schema.NestedAttributeObject{
					CustomType: ListNestedAttributeAssocExtType{
						ObjectType: types.ObjectType{
							AttrTypes: map[string]attr.Type{
								"int64_attribute": types.Int64Type,
							},
						},
					},
					Attributes: map[string]schema.Attribute{
						"int64_attribute": schema.Int64Attribute{
							Optional: true,
						},
					},
				},
			},
			/* ... */

type exampleResourceData struct {
	ListNestedAttributeAssocExtType types.List `tfsdk:"list_nested_attribute_assoc_ext_type"`
}

Terraform Configuration Files

resource "example_resource" "example" {
  list_nested_attribute_assoc_ext_type = [
    {
      int64_attribute = 1
    },
    {
      int64_attribute = 2
    }
  ]
}

Debug Output

https://gist.github.com/bendbennett/df35a80597f6b9df94a6a29793668695

Expected Behavior

terraform apply should run successfully.

Actual Behavior

terraform apply generates the following error:

│ Error: Invalid List Element Type
│ 
│   with example_resource.example,
│   on resource.tf line 9, in resource "example_resource" "example":
│    9: resource "example_resource" "example" {
│ 
│ While creating a List value, an invalid element was detected. A List must use the single, given element type. This is always an issue with the provider and should be reported to the provider developers.
│ 
│ List Element Type: CustomObjectType
│ List Index (0) Element Type: types.ObjectType["int64_attribute":basetypes.Int64Type]

Steps to Reproduce

  1. terraform apply

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions