Handle multi-element arrays in flattenedEncoder and flattenedDecoder #3241
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug where arrays/lists were truncated to only the first element in the Go SDK during Read operations. This only affected the Go SDK - Python, TypeScript, and .NET SDKs worked correctly.
Fixes: #3240
Problem
When using Plugin Framework providers with
ListNestedBlockorSetNestedBlocktypes, the Go SDK would:Example (Twingate provider)
Before fix:
{ "inputs": { "accessServices": [2 elements], "accessGroups": [2 elements] }, "outputs": { "accessServices": [1 element], // ❌ Truncated! "accessGroups": [1 element] // ❌ Truncated! } }After fix:
{ "inputs": { "accessServices": [2 elements], "accessGroups": [2 elements] }, "outputs": { "accessServices": [2 elements], // ✅ Fixed! "accessGroups": [2 elements] // ✅ Fixed! } }Root Cause
The bug occurred in two places in
pkg/convert/flattened.go:flattenedEncoder.fromPropertyValue()(lines 29-58): Only handled single elements, not arrays. When encoding multi-element arrays, only the first element was sent to the Terraform provider.flattenedDecoder.toPropertyValue()(lines 58-74): Returned an error when receiving >1 elements. This error was swallowed somewhere in the chain, causing only the first element to be preserved in Go SDK outputs.These encoders/decoders are selected when
IsMaxItemsOne()incorrectly identifies a multi-item list as single-item. This commonly occurs with Plugin Framework providers usingListNestedBlockorSetNestedBlock.The Fix
Encoder Fix
v.IsArray()Decoder Fix
resource.NewArrayProperty(values)instead of erroringTesting
Tested with the Twingate Pulumi provider, which uses Plugin Framework
ListNestedBlocktypes:Test Results
Before fix:
After fix:
types.Listandtypes.SetListNestedBlockandSetNestedBlock