Reworked logic to check extensions were not caught either.

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2024-02-06 15:34:01 -05:00
parent 103f2dfb0b
commit 9db2900c16
2 changed files with 74 additions and 0 deletions

View File

@@ -96,6 +96,29 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
// https://github.com/pb33f/libopenapi/issues/76
mapOfSchemaContainingNodes := []string{"properties", "patternProperties"}
if i%2 == 0 && slices.Contains(mapOfSchemaContainingNodes, n.Value) && !utils.IsNodeArray(node) && (i+1 < len(node.Content)) {
// if 'examples' or 'example' exists in the seenPath, skip this 'properties' node.
// https://github.com/pb33f/libopenapi/issues/160
if len(seenPath) > 0 {
skip := false
// iterate through the path and look for an item named 'examples' or 'example'
for _, p := range seenPath {
if p == "examples" || p == "example" {
skip = true
break
}
// look for any extension in the path that begins with 'x-'
if strings.HasPrefix(p, "x-") {
skip = true
break
}
}
if skip {
continue
}
}
// for each property add it to our schema definitions
label := ""
for h, prop := range node.Content[i+1].Content {

View File

@@ -1650,3 +1650,54 @@ components:
assert.Equal(t, "bing bong", params["/test"]["top"]["#/components/parameters/test-2"][0].Node.Content[5].Value)
assert.Equal(t, "ding a ling", params["/test"]["get"]["#/components/parameters/test-3"][0].Node.Content[5].Value)
}
func TestSpecIndex_CheckPropertiesFromExamplesIgnored(t *testing.T) {
yml := `paths:
/test:
get:
responses:
'200':
description: OK
x-properties:
properties:
name: not a schema
x-example:
properties:
name: not a schema
content:
application/json:
schema:
$ref: "#/components/schemas/Object"
examples:
Example1:
value:
properties:
name: not a schema
Example2:
value:
properties:
name: another one
components:
schemas:
Object:
type: object
x-properties:
properties:
name: not a schema
properties:
properties:
type: object
properties:
name:
type: string
example:
properties:
name: not a schema`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
index := NewSpecIndexWithConfig(&rootNode, CreateOpenAPIIndexConfig())
schemas := index.GetAllSchemas()
assert.Equal(t, 6, len(schemas))
}