From 74bdd3bbe5066536052e94729214efe6574551de Mon Sep 17 00:00:00 2001 From: Aleksandar Stojanov Date: Mon, 11 Nov 2024 12:04:43 +0100 Subject: [PATCH] feature: merge nested arrays When sequence node has items that are arrays, we assumed that those arrays have same set of elements so only the first item was analysed. Now, that is no longer the case and all those arrays are merged into single one before determining their types. closes #99 Signed-off-by: Aleksandar Stojanov --- pkg/schema.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/schema.go b/pkg/schema.go index 991d320..b441fa2 100644 --- a/pkg/schema.go +++ b/pkg/schema.go @@ -261,13 +261,20 @@ func parseNode(keyNode *yaml.Node, valNode *yaml.Node) (*Schema, bool) { case yaml.SequenceNode: schema.Type = "array" - if len(valNode.Content) > 0 { - itemSchema, _ := parseNode(nil, valNode.Content[0]) + + mergedItemSchema := &Schema{} + + for _, itemNode := range valNode.Content { + itemSchema, _ := parseNode(nil, itemNode) if itemSchema != nil && !itemSchema.Hidden { - schema.Items = itemSchema + mergedItemSchema = mergeSchemas(mergedItemSchema, itemSchema) } } + if mergedItemSchema != nil { + schema.Items = mergedItemSchema + } + case yaml.ScalarNode: if valNode.Style == yaml.DoubleQuotedStyle || valNode.Style == yaml.SingleQuotedStyle { schema.Type = "string"