Skip to content

Commit

Permalink
Treat empty blocks with MinItems == 0 as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ovandriyanov authored and detro committed May 3, 2022
1 parent 98278c6 commit e428476
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion schemamd/behaviors.go
Expand Up @@ -16,12 +16,17 @@ func childAttributeIsOptional(att *tfjson.SchemaAttribute) bool {
return att.Optional
}

// childBlockIsOptional returns true for blocks with with min items 0 and any required or optional children.
// childBlockIsOptional returns true for blocks with with min items 0
// which are either empty or have any required or optional children.
func childBlockIsOptional(block *tfjson.SchemaBlockType) bool {
if block.MinItems > 0 {
return false
}

if len(block.Block.NestedBlocks) == 0 && len(block.Block.Attributes) == 0 {
return true
}

for _, childBlock := range block.Block.NestedBlocks {
if childBlockIsRequired(childBlock) {
return true
Expand Down
10 changes: 10 additions & 0 deletions schemamd/behaviors_test.go
Expand Up @@ -324,6 +324,16 @@ func TestChildBlockIsOptional(t *testing.T) {
},
false,
},
{
"empty block",
&tfjson.SchemaBlockType{
NestingMode: tfjson.SchemaNestingModeSingle,
Block: &tfjson.SchemaBlock{
Description: "This is an empty block.",
},
},
true,
},
} {
t.Run(c.name, func(t *testing.T) {
actual := childBlockIsOptional(c.block)
Expand Down

0 comments on commit e428476

Please sign in to comment.