Skip to content

Commit 0a567c5

Browse files
ovandriyanovIvan De Marino
authored andcommitted
Treat empty blocks with MinItems == 0 as optional
1 parent 98278c6 commit 0a567c5

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

schemamd/behaviors.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ func childAttributeIsOptional(att *tfjson.SchemaAttribute) bool {
1616
return att.Optional
1717
}
1818

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

26+
if len(block.Block.NestedBlocks) == 0 && len(block.Block.Attributes) == 0 {
27+
return true
28+
}
29+
2530
for _, childBlock := range block.Block.NestedBlocks {
2631
if childBlockIsRequired(childBlock) {
2732
return true

schemamd/behaviors_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@ func TestChildBlockIsOptional(t *testing.T) {
324324
},
325325
false,
326326
},
327+
{
328+
"empty block",
329+
&tfjson.SchemaBlockType{
330+
NestingMode: tfjson.SchemaNestingModeSingle,
331+
Block: &tfjson.SchemaBlock{
332+
Description: "This is an empty block.",
333+
},
334+
},
335+
true,
336+
},
327337
} {
328338
t.Run(c.name, func(t *testing.T) {
329339
actual := childBlockIsOptional(c.block)

0 commit comments

Comments
 (0)