diff --git a/schemamd/behaviors.go b/schemamd/behaviors.go index 88fd7b01..954225ea 100644 --- a/schemamd/behaviors.go +++ b/schemamd/behaviors.go @@ -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 diff --git a/schemamd/behaviors_test.go b/schemamd/behaviors_test.go index d9142950..3bdb087f 100644 --- a/schemamd/behaviors_test.go +++ b/schemamd/behaviors_test.go @@ -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)