updated: a fix for #50

properties are now checked as part of the inline schemas. paths updated also to ensure correctness.
This commit is contained in:
Dave Shanley
2023-02-27 10:29:01 -05:00
parent bf4e94bcbd
commit 6d384b2732
2 changed files with 63 additions and 1 deletions

View File

@@ -47,7 +47,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
}
ref := &Reference{
Node: node.Content[i+1],
Path: fmt.Sprintf("$.%s", strings.Join(seenPath, ".")),
Path: fmt.Sprintf("$.%s.schema", strings.Join(seenPath, ".")),
}
index.allInlineSchemaDefinitions = append(index.allInlineSchemaDefinitions, ref)
@@ -61,6 +61,40 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
}
}
// Perform the same check for all properties in an inline schema definition
// https://github.com/pb33f/libopenapi/issues/76
if i%2 == 0 && n.Value == "properties" && !utils.IsNodeArray(node) && (i+1 < len(node.Content)) {
isRef, _, _ := utils.IsNodeRefValue(node.Content[i+1])
if isRef {
continue
}
// for each property add it to our schema definitions
label := ""
for h, prop := range node.Content[i+1].Content {
if h%2 == 0 {
label = prop.Value
continue
}
ref := &Reference{
Node: prop,
Path: fmt.Sprintf("$.%s.properties.%s", strings.Join(seenPath, "."), label),
}
index.allInlineSchemaDefinitions = append(index.allInlineSchemaDefinitions, ref)
// check if the schema is an object or an array,
// and if so, add it to the list of inline schema object definitions.
k, v := utils.FindKeyNodeTop("type", node.Content[i+1].Content)
if k != nil && v != nil {
if v.Value == "object" || v.Value == "array" {
index.allInlineSchemaObjectDefinitions = append(index.allInlineSchemaObjectDefinitions, ref)
}
}
}
}
if i%2 == 0 && n.Value == "$ref" {
// only look at scalar values, not maps (looking at you k8s)

View File

@@ -35,3 +35,31 @@ paths:
assert.Len(t, idx.allDescriptions, 2)
assert.Equal(t, 2, idx.descriptionCount)
}
func TestSpecIndex_ExtractRefs_CheckPropertiesForInlineSchema(t *testing.T) {
yml := `openapi: 3.1.0
servers:
- url: http://localhost:8080
paths:
/test:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
test:
type: array
items: true
`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allInlineSchemaDefinitions, 2)
assert.Len(t, idx.allInlineSchemaObjectDefinitions, 1)
}