From 9966c35e220869d6d3bfbb8a746c05c40fc72071 Mon Sep 17 00:00:00 2001 From: quobix Date: Wed, 12 Feb 2025 14:29:32 -0500 Subject: [PATCH] Addressed issue #615 do not modify sequence nodes! addressed #615 do not modify sequence nodes. --- functions/openapi/examples_schema.go | 2 +- functions/openapi/examples_schema_test.go | 52 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/functions/openapi/examples_schema.go b/functions/openapi/examples_schema.go index f5acf65a..4c33a0a2 100644 --- a/functions/openapi/examples_schema.go +++ b/functions/openapi/examples_schema.go @@ -277,7 +277,7 @@ func changeKeys(depth int, node *yaml.Node) { if i%2 != 0 { continue // keys only. } - if no.Tag != "!!str" { + if node.Tag != "!!seq" && no.Tag != "!!str" { no.Tag = "!!str" } if len(no.Content) > 0 { diff --git a/functions/openapi/examples_schema_test.go b/functions/openapi/examples_schema_test.go index f28a9dfa..929abfca 100644 --- a/functions/openapi/examples_schema_test.go +++ b/functions/openapi/examples_schema_test.go @@ -506,6 +506,20 @@ paths: } +/* +components: + schemas: + Test: + type: array + description: Test array with numbers + items: + type: number + example: + - 0 # <- This gives a warning + - 0 + - 0 +*/ + func TestExamplesSchema_HandleJSONTime(t *testing.T) { yml := `openapi: 3.1 components: @@ -538,3 +552,41 @@ components: assert.Len(t, res, 0) } + +// https://github.com/daveshanley/vacuum/issues/615 +func TestExamplesSchema_HandleArrays(t *testing.T) { + yml := `openapi: 3.1.0 +components: + schemas: + Test: + type: array + description: Test array with numbers + items: + type: number + example: + - 0 + - 0 + - 0` + + document, err := libopenapi.NewDocument([]byte(yml)) + if err != nil { + panic(fmt.Sprintf("cannot create new document: %e", err)) + } + + m, _ := document.BuildV3Model() + path := "$" + + drDocument := drModel.NewDrDocument(m) + + rule := buildOpenApiTestRuleAction(path, "examples_schema", "", nil) + ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) + + ctx.Document = document + ctx.DrDocument = drDocument + ctx.Rule = &rule + + def := ExamplesSchema{} + res := def.RunRule(nil, ctx) + + assert.Len(t, res, 0) +}