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

fix: fix data race in schema merging logic by creating a copy instead of modifying the original struct instance #397

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions decoder/internal/schemahelper/block_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,24 @@ func MergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*
}
for bType, block := range depSchema.Blocks {
if _, exists := mergedSchema.Blocks[bType]; !exists {
copiedBlock := block.Copy()
// propagate DynamicBlocks extension to any nested blocks
if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks {
if block.Body.Extensions == nil {
block.Body.Extensions = &schema.BodyExtensions{}
if copiedBlock.Body.Extensions == nil {
copiedBlock.Body.Extensions = &schema.BodyExtensions{}
}
block.Body.Extensions.DynamicBlocks = true
copiedBlock.Body.Extensions.DynamicBlocks = true
}

mergedSchema.Blocks[bType] = block
mergedSchema.Blocks[bType] = copiedBlock
} else {
// Skip duplicate block type
continue
}
}

if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks && len(depSchema.Blocks) > 0 {
mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(depSchema)
mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(depSchema, mergedSchema)
}

mergedSchema.TargetableAs = append(mergedSchema.TargetableAs, depSchema.TargetableAs...)
Expand Down Expand Up @@ -87,7 +88,7 @@ func MergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*
}
}

mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(mergedSchema)
mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(mergedSchema, mergedSchema)
}

return mergedSchema, result
Expand Down
10 changes: 8 additions & 2 deletions decoder/internal/schemahelper/dynamic_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import (
"github.com/zclconf/go-cty/cty"
)

func buildDynamicBlockSchema(inputSchema *schema.BodySchema) *schema.BlockSchema {
// buildDynamicBlockSchema creates the schema for dynamic blocks based on the available blocks in the body schema (inputSchema).
//
// inputSchema - schema used to figure out which blocks can be dynamic (commonly the dependent schema in order to NOT include static blocks like e.g. lifecycle)
// sourceSchema - schema used to copy the body of the block (commonly the merged schema in order to include changes that were made to the schema)
func buildDynamicBlockSchema(inputSchema *schema.BodySchema, sourceSchema *schema.BodySchema) *schema.BlockSchema {
dependentBody := make(map[schema.SchemaKey]*schema.BodySchema)
for blockName, block := range inputSchema.Blocks {
for blockName := range inputSchema.Blocks {
block := sourceSchema.Blocks[blockName]

dependentBody[schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: blockName},
Expand Down