Skip to content

Commit

Permalink
internal/fwserver: Ensure Attribute and Block Plan Modification Retur…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
bendbennett authored Aug 17, 2023
1 parent 9aa85ed commit 4ee15c8
Show file tree
Hide file tree
Showing 12 changed files with 2,927 additions and 88 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230816-113531.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'internal/fwserver: Prevented `Invalid Element Type` diagnostics for nested
attributes and blocks implementing `CustomType` field'
time: 2023-08-16T11:35:31.553124+01:00
custom:
Issue: "823"
111 changes: 108 additions & 3 deletions internal/fwserver/attribute_plan_modification.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt
return
}

planObjectValuable, diags := coerceObjectValuable(ctx, attrPath, planElem)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

typable, diags := coerceObjectTypable(ctx, attrPath, planObjectValuable)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

stateObject, diags := listElemObject(ctx, attrPath, stateList, idx, fwschemadata.DataDescriptionState)

resp.Diagnostics.Append(diags...)
Expand All @@ -219,7 +235,26 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt

NestedAttributeObjectPlanModify(ctx, nestedAttributeObject, objectReq, objectResp)

planElements[idx] = objectResp.AttributePlan
respValue, diags := coerceObjectValue(ctx, attrPath, objectResp.AttributePlan)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

// A custom value type must be returned in the final response to prevent
// later correctness errors.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/821
respValuable, diags := typable.ValueFromObject(ctx, respValue)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

planElements[idx] = respValuable
resp.Diagnostics.Append(objectResp.Diagnostics...)
resp.Private = objectResp.Private
resp.RequiresReplace.Append(objectResp.RequiresReplace...)
Expand Down Expand Up @@ -309,6 +344,22 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt
return
}

planObjectValuable, diags := coerceObjectValuable(ctx, attrPath, planElem)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

typable, diags := coerceObjectTypable(ctx, attrPath, planObjectValuable)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

stateObject, diags := setElemObject(ctx, attrPath, stateSet, idx, fwschemadata.DataDescriptionState)

resp.Diagnostics.Append(diags...)
Expand All @@ -335,7 +386,26 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt

NestedAttributeObjectPlanModify(ctx, nestedAttributeObject, objectReq, objectResp)

planElements[idx] = objectResp.AttributePlan
respValue, diags := coerceObjectValue(ctx, attrPath, objectResp.AttributePlan)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

// A custom value type must be returned in the final response to prevent
// later correctness errors.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/821
respValuable, diags := typable.ValueFromObject(ctx, respValue)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

planElements[idx] = respValuable
resp.Diagnostics.Append(objectResp.Diagnostics...)
resp.Private = objectResp.Private
resp.RequiresReplace.Append(objectResp.RequiresReplace...)
Expand Down Expand Up @@ -425,6 +495,22 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt
return
}

planObjectValuable, diags := coerceObjectValuable(ctx, attrPath, planElem)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

typable, diags := coerceObjectTypable(ctx, attrPath, planObjectValuable)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

stateObject, diags := mapElemObject(ctx, attrPath, stateMap, key, fwschemadata.DataDescriptionState)

resp.Diagnostics.Append(diags...)
Expand All @@ -451,7 +537,26 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt

NestedAttributeObjectPlanModify(ctx, nestedAttributeObject, objectReq, objectResp)

planElements[key] = objectResp.AttributePlan
respValue, diags := coerceObjectValue(ctx, attrPath, objectResp.AttributePlan)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

// A custom value type must be returned in the final response to prevent
// later correctness errors.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/821
respValuable, diags := typable.ValueFromObject(ctx, respValue)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

planElements[key] = respValuable
resp.Diagnostics.Append(objectResp.Diagnostics...)
resp.Private = objectResp.Private
resp.RequiresReplace.Append(objectResp.RequiresReplace...)
Expand Down
Loading

0 comments on commit 4ee15c8

Please sign in to comment.