Skip to content

Commit 4ee15c8

Browse files
authored
internal/fwserver: Ensure Attribute and Block Plan Modification Returns 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)
1 parent 9aa85ed commit 4ee15c8

12 files changed

+2927
-88
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'internal/fwserver: Prevented `Invalid Element Type` diagnostics for nested
3+
attributes and blocks implementing `CustomType` field'
4+
time: 2023-08-16T11:35:31.553124+01:00
5+
custom:
6+
Issue: "823"

internal/fwserver/attribute_plan_modification.go

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,22 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt
193193
return
194194
}
195195

196+
planObjectValuable, diags := coerceObjectValuable(ctx, attrPath, planElem)
197+
198+
resp.Diagnostics.Append(diags...)
199+
200+
if resp.Diagnostics.HasError() {
201+
return
202+
}
203+
204+
typable, diags := coerceObjectTypable(ctx, attrPath, planObjectValuable)
205+
206+
resp.Diagnostics.Append(diags...)
207+
208+
if resp.Diagnostics.HasError() {
209+
return
210+
}
211+
196212
stateObject, diags := listElemObject(ctx, attrPath, stateList, idx, fwschemadata.DataDescriptionState)
197213

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

220236
NestedAttributeObjectPlanModify(ctx, nestedAttributeObject, objectReq, objectResp)
221237

222-
planElements[idx] = objectResp.AttributePlan
238+
respValue, diags := coerceObjectValue(ctx, attrPath, objectResp.AttributePlan)
239+
240+
resp.Diagnostics.Append(diags...)
241+
242+
if resp.Diagnostics.HasError() {
243+
return
244+
}
245+
246+
// A custom value type must be returned in the final response to prevent
247+
// later correctness errors.
248+
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/821
249+
respValuable, diags := typable.ValueFromObject(ctx, respValue)
250+
251+
resp.Diagnostics.Append(diags...)
252+
253+
if resp.Diagnostics.HasError() {
254+
return
255+
}
256+
257+
planElements[idx] = respValuable
223258
resp.Diagnostics.Append(objectResp.Diagnostics...)
224259
resp.Private = objectResp.Private
225260
resp.RequiresReplace.Append(objectResp.RequiresReplace...)
@@ -309,6 +344,22 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt
309344
return
310345
}
311346

347+
planObjectValuable, diags := coerceObjectValuable(ctx, attrPath, planElem)
348+
349+
resp.Diagnostics.Append(diags...)
350+
351+
if resp.Diagnostics.HasError() {
352+
return
353+
}
354+
355+
typable, diags := coerceObjectTypable(ctx, attrPath, planObjectValuable)
356+
357+
resp.Diagnostics.Append(diags...)
358+
359+
if resp.Diagnostics.HasError() {
360+
return
361+
}
362+
312363
stateObject, diags := setElemObject(ctx, attrPath, stateSet, idx, fwschemadata.DataDescriptionState)
313364

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

336387
NestedAttributeObjectPlanModify(ctx, nestedAttributeObject, objectReq, objectResp)
337388

338-
planElements[idx] = objectResp.AttributePlan
389+
respValue, diags := coerceObjectValue(ctx, attrPath, objectResp.AttributePlan)
390+
391+
resp.Diagnostics.Append(diags...)
392+
393+
if resp.Diagnostics.HasError() {
394+
return
395+
}
396+
397+
// A custom value type must be returned in the final response to prevent
398+
// later correctness errors.
399+
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/821
400+
respValuable, diags := typable.ValueFromObject(ctx, respValue)
401+
402+
resp.Diagnostics.Append(diags...)
403+
404+
if resp.Diagnostics.HasError() {
405+
return
406+
}
407+
408+
planElements[idx] = respValuable
339409
resp.Diagnostics.Append(objectResp.Diagnostics...)
340410
resp.Private = objectResp.Private
341411
resp.RequiresReplace.Append(objectResp.RequiresReplace...)
@@ -425,6 +495,22 @@ func AttributeModifyPlan(ctx context.Context, a fwschema.Attribute, req ModifyAt
425495
return
426496
}
427497

498+
planObjectValuable, diags := coerceObjectValuable(ctx, attrPath, planElem)
499+
500+
resp.Diagnostics.Append(diags...)
501+
502+
if resp.Diagnostics.HasError() {
503+
return
504+
}
505+
506+
typable, diags := coerceObjectTypable(ctx, attrPath, planObjectValuable)
507+
508+
resp.Diagnostics.Append(diags...)
509+
510+
if resp.Diagnostics.HasError() {
511+
return
512+
}
513+
428514
stateObject, diags := mapElemObject(ctx, attrPath, stateMap, key, fwschemadata.DataDescriptionState)
429515

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

452538
NestedAttributeObjectPlanModify(ctx, nestedAttributeObject, objectReq, objectResp)
453539

454-
planElements[key] = objectResp.AttributePlan
540+
respValue, diags := coerceObjectValue(ctx, attrPath, objectResp.AttributePlan)
541+
542+
resp.Diagnostics.Append(diags...)
543+
544+
if resp.Diagnostics.HasError() {
545+
return
546+
}
547+
548+
// A custom value type must be returned in the final response to prevent
549+
// later correctness errors.
550+
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/821
551+
respValuable, diags := typable.ValueFromObject(ctx, respValue)
552+
553+
resp.Diagnostics.Append(diags...)
554+
555+
if resp.Diagnostics.HasError() {
556+
return
557+
}
558+
559+
planElements[key] = respValuable
455560
resp.Diagnostics.Append(objectResp.Diagnostics...)
456561
resp.Private = objectResp.Private
457562
resp.RequiresReplace.Append(objectResp.RequiresReplace...)

0 commit comments

Comments
 (0)