diff --git a/index/extract_refs.go b/index/extract_refs.go index c6a986f..bcae9dc 100644 --- a/index/extract_refs.go +++ b/index/extract_refs.go @@ -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 { diff --git a/index/spec_index_test.go b/index/spec_index_test.go index 2ceae38..208e5ad 100644 --- a/index/spec_index_test.go +++ b/index/spec_index_test.go @@ -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)) +}