Skip to content
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

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

Closed
bendbennett opened this issue Aug 15, 2023 · 1 comment · Fixed by #823
Labels
bug Something isn't working
Milestone

Comments

@bendbennett
Copy link
Contributor

bendbennett commented Aug 15, 2023

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

@bendbennett bendbennett added the bug Something isn't working label Aug 15, 2023
@bendbennett bendbennett added this to the v1.3.5 milestone Aug 15, 2023
bendbennett added a commit that referenced this issue Aug 15, 2023
…ype implementations for list, map, set nested attributes using a nested object with a custom type (#821)

Reference: #768
Reference: #767
bendbennett added a commit that referenced this issue Aug 16, 2023
…implementations for list, set nested blocks using a nested object with a custom type (#821)

Reference: #768
Reference: #767
bendbennett added a commit that referenced this issue Aug 16, 2023
bendbennett added a commit that referenced this issue Aug 17, 2023
…ns Custom Value Type Implementations When Using Custom Type NestedAttributeObject/NestedBlockObject (#823)

* internal/fwserver: Attribute plan modification returns custom value type implementations for list, map, set nested attributes using a nested object with a custom type (#821)

Reference: #768
Reference: #767

* internal/fwserver: Block plan modification returns custom value type implementations for list, set nested blocks using a nested object with a custom type (#821)

Reference: #768
Reference: #767

* Using testschema throughout tests for attribute and block plan modifier (#821)

* Renaming attr and updating tests (#821)

* Add changelog entry (#821)

* Removing hardcoded AttrTypes (#821)
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
1 participant