Skip to content

Commit 74c7a3f

Browse files
fix: improves the ability to get all inline schemas and identify inline object schemas (#154)
* fix: improves the ability to get all inline schemas and identify inline object schemas * fix * fix * fix * fix
1 parent 976ca09 commit 74c7a3f

5 files changed

Lines changed: 124 additions & 35 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.20
55
require (
66
github.com/stretchr/testify v1.8.0
77
github.com/vmware-labs/yaml-jsonpath v0.3.2
8+
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
89
golang.org/x/sync v0.1.0
910
gopkg.in/yaml.v3 v3.0.1
1011
)

go.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
6464
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6565
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
6666
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
67+
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb h1:mIKbk8weKhSeLH2GmUTrvx8CjkyJmnU1wFmg59CUjFA=
68+
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
6769
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
6870
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
6971
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -91,8 +93,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
9193
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9294
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9395
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
94-
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
9596
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
97+
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
9698
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
9799
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
98100
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

index/extract_refs.go

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package index
66
import (
77
"errors"
88
"fmt"
9+
"strings"
10+
911
"github.com/pb33f/libopenapi/utils"
12+
"golang.org/x/exp/slices"
1013
"gopkg.in/yaml.v3"
11-
"strings"
1214
)
1315

1416
// ExtractRefs will return a deduplicated slice of references for every unique ref found in the document.
@@ -40,21 +42,26 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
4042
// check if we're dealing with an inline schema definition, that isn't part of an array
4143
// (which means it's being used as a value in an array, and it's not a label)
4244
// https://github.com/pb33f/libopenapi/issues/76
43-
if i%2 == 0 && n.Value == "schema" && !utils.IsNodeArray(node) && (i+1 < len(node.Content)) {
45+
schemaContainingNodes := []string{"schema", "items", "additionalProperties", "contains", "not", "unevaluatedItems", "unevaluatedProperties"}
46+
if i%2 == 0 && slices.Contains(schemaContainingNodes, n.Value) && !utils.IsNodeArray(node) && (i+1 < len(node.Content)) {
47+
ref := &Reference{
48+
Node: node.Content[i+1],
49+
Path: fmt.Sprintf("$.%s.%s", strings.Join(seenPath, "."), n.Value),
50+
}
51+
4452
isRef, _, _ := utils.IsNodeRefValue(node.Content[i+1])
4553
if isRef {
4654
// record this reference
47-
ref := &Reference{
48-
Node: node.Content[i+1],
49-
Path: fmt.Sprintf("$.%s.schema", strings.Join(seenPath, ".")),
50-
}
5155
index.allRefSchemaDefinitions = append(index.allRefSchemaDefinitions, ref)
5256
continue
5357
}
54-
ref := &Reference{
55-
Node: node.Content[i+1],
56-
Path: fmt.Sprintf("$.%s.schema", strings.Join(seenPath, ".")),
58+
59+
if n.Value == "additionalProperties" || n.Value == "unevaluatedProperties" {
60+
if utils.IsNodeBoolValue(node.Content[i+1]) {
61+
continue
62+
}
5763
}
64+
5865
index.allInlineSchemaDefinitions = append(index.allInlineSchemaDefinitions, ref)
5966

6067
// check if the schema is an object or an array,
@@ -67,14 +74,10 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
6774
}
6875
}
6976

70-
// Perform the same check for all properties in an inline schema definition
77+
// Perform the same check for all maps of schemas like properties and patternProperties
7178
// https://github.com/pb33f/libopenapi/issues/76
72-
if i%2 == 0 && n.Value == "properties" && !utils.IsNodeArray(node) && (i+1 < len(node.Content)) {
73-
isRef, _, _ := utils.IsNodeRefValue(node.Content[i+1])
74-
if isRef {
75-
continue
76-
}
77-
79+
mapOfSchemaContainingNodes := []string{"properties", "patternProperties"}
80+
if i%2 == 0 && slices.Contains(mapOfSchemaContainingNodes, n.Value) && !utils.IsNodeArray(node) && (i+1 < len(node.Content)) {
7881
// for each property add it to our schema definitions
7982
label := ""
8083
for h, prop := range node.Content[i+1].Content {
@@ -83,16 +86,51 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
8386
label = prop.Value
8487
continue
8588
}
86-
8789
ref := &Reference{
8890
Node: prop,
89-
Path: fmt.Sprintf("$.%s.properties.%s", strings.Join(seenPath, "."), label),
91+
Path: fmt.Sprintf("$.%s.%s.%s", strings.Join(seenPath, "."), n.Value, label),
92+
}
93+
94+
isRef, _, _ := utils.IsNodeRefValue(prop)
95+
if isRef {
96+
// record this reference
97+
index.allRefSchemaDefinitions = append(index.allRefSchemaDefinitions, ref)
98+
continue
99+
}
100+
101+
index.allInlineSchemaDefinitions = append(index.allInlineSchemaDefinitions, ref)
102+
103+
// check if the schema is an object or an array,
104+
// and if so, add it to the list of inline schema object definitions.
105+
k, v := utils.FindKeyNodeTop("type", prop.Content)
106+
if k != nil && v != nil {
107+
if v.Value == "object" || v.Value == "array" {
108+
index.allInlineSchemaObjectDefinitions = append(index.allInlineSchemaObjectDefinitions, ref)
109+
}
110+
}
111+
}
112+
}
113+
114+
// Perform the same check for all arrays of schemas like allOf, anyOf, oneOf
115+
arrayOfSchemaContainingNodes := []string{"allOf", "anyOf", "oneOf", "prefixItems"}
116+
if i%2 == 0 && slices.Contains(arrayOfSchemaContainingNodes, n.Value) && !utils.IsNodeArray(node) && (i+1 < len(node.Content)) {
117+
// for each element in the array, add it to our schema definitions
118+
for h, element := range node.Content[i+1].Content {
119+
ref := &Reference{
120+
Node: element,
121+
Path: fmt.Sprintf("$.%s.%s[%d]", strings.Join(seenPath, "."), n.Value, h),
122+
}
123+
124+
isRef, _, _ := utils.IsNodeRefValue(element)
125+
if isRef { // record this reference
126+
index.allRefSchemaDefinitions = append(index.allRefSchemaDefinitions, ref)
127+
continue
90128
}
91129
index.allInlineSchemaDefinitions = append(index.allInlineSchemaDefinitions, ref)
92130

93131
// check if the schema is an object or an array,
94132
// and if so, add it to the list of inline schema object definitions.
95-
k, v := utils.FindKeyNodeTop("type", node.Content[i+1].Content)
133+
k, v := utils.FindKeyNodeTop("type", element.Content)
96134
if k != nil && v != nil {
97135
if v.Value == "object" || v.Value == "array" {
98136
index.allInlineSchemaObjectDefinitions = append(index.allInlineSchemaObjectDefinitions, ref)
@@ -342,7 +380,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
342380
if i < len(node.Content)-1 {
343381
next := node.Content[i+1]
344382

345-
if i%2 != 0 && next != nil && !utils.IsNodeArray(next) && !utils.IsNodeMap(next) {
383+
if i%2 != 0 && next != nil && !utils.IsNodeArray(next) && !utils.IsNodeMap(next) && len(seenPath) > 0 {
346384
seenPath = seenPath[:len(seenPath)-1]
347385
}
348386
}
@@ -366,7 +404,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
366404
func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Reference {
367405
var found []*Reference
368406

369-
//run this async because when things get recursive, it can take a while
407+
// run this async because when things get recursive, it can take a while
370408
c := make(chan bool)
371409

372410
locate := func(ref *Reference, refIndex int, sequence []*ReferenceMapped) {
@@ -419,7 +457,7 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
419457
for r := range refsToCheck {
420458
// expand our index of all mapped refs
421459
go locate(refsToCheck[r], r, mappedRefsInSequence)
422-
//locate(refsToCheck[r], r, mappedRefsInSequence) // used for sync testing.
460+
// locate(refsToCheck[r], r, mappedRefsInSequence) // used for sync testing.
423461
}
424462

425463
completedRefs := 0

index/extract_refs_test.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
package index
55

66
import (
7+
"testing"
8+
79
"github.com/stretchr/testify/assert"
810
"gopkg.in/yaml.v3"
9-
"testing"
1011
)
1112

1213
func TestSpecIndex_ExtractRefs_CheckDescriptionNotMap(t *testing.T) {
13-
1414
yml := `openapi: 3.1.0
1515
info:
1616
description: This is a description
@@ -37,7 +37,6 @@ paths:
3737
}
3838

3939
func TestSpecIndex_ExtractRefs_CheckPropertiesForInlineSchema(t *testing.T) {
40-
4140
yml := `openapi: 3.1.0
4241
servers:
4342
- url: http://localhost:8080
@@ -54,14 +53,63 @@ paths:
5453
properties:
5554
test:
5655
type: array
57-
items: true
56+
items:
57+
type: object
58+
prefixItems:
59+
- $ref: '#/components/schemas/Test'
60+
additionalProperties: false
61+
unevaluatedProperties: false
62+
components:
63+
schemas:
64+
Test:
65+
type: object
66+
additionalProperties:
67+
type: string
68+
contains:
69+
type: string
70+
not:
71+
type: number
72+
unevaluatedProperties:
73+
type: boolean
74+
patternProperties:
75+
^S_:
76+
type: string
77+
^I_:
78+
type: integer
79+
prefixItems:
80+
- type: string
81+
AllOf:
82+
allOf:
83+
- type: object
84+
properties:
85+
test:
86+
type: string
87+
- type: object
88+
properties:
89+
test2:
90+
type: string
91+
AnyOf:
92+
anyOf:
93+
- type: object
94+
properties:
95+
test:
96+
type: string
97+
- type: object
98+
properties:
99+
test2:
100+
type: string
101+
OneOf:
102+
oneOf:
103+
- type: string
104+
- type: number
105+
- type: boolean
58106
`
59107
var rootNode yaml.Node
60108
_ = yaml.Unmarshal([]byte(yml), &rootNode)
61109
c := CreateOpenAPIIndexConfig()
62110
idx := NewSpecIndexWithConfig(&rootNode, c)
63-
assert.Len(t, idx.allInlineSchemaDefinitions, 2)
64-
assert.Len(t, idx.allInlineSchemaObjectDefinitions, 1)
111+
assert.Len(t, idx.allInlineSchemaDefinitions, 21)
112+
assert.Len(t, idx.allInlineSchemaObjectDefinitions, 7)
65113
}
66114

67115
// https://github.com/pb33f/libopenapi/issues/112

index/spec_index_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestSpecIndex_ExtractRefsStripe(t *testing.T) {
4444
assert.Len(t, index.GetPolyAllOfReferences(), 0)
4545
assert.Len(t, index.GetPolyOneOfReferences(), 275)
4646
assert.Len(t, index.GetPolyAnyOfReferences(), 553)
47-
assert.Len(t, index.GetAllReferenceSchemas(), 696)
47+
assert.Len(t, index.GetAllReferenceSchemas(), 1972)
4848
assert.NotNil(t, index.GetRootServersNode())
4949
assert.Len(t, index.GetAllRootServers(), 1)
5050

@@ -283,7 +283,7 @@ func TestSpecIndex_BurgerShop(t *testing.T) {
283283
assert.Equal(t, 6, index.GetPathCount())
284284

285285
assert.Equal(t, 6, len(index.GetAllComponentSchemas()))
286-
assert.Equal(t, 47, len(index.GetAllSchemas()))
286+
assert.Equal(t, 56, len(index.GetAllSchemas()))
287287

288288
assert.Equal(t, 34, len(index.GetAllSequencedReferences()))
289289
assert.NotNil(t, index.GetSchemasNode())
@@ -1096,10 +1096,10 @@ func ExampleNewSpecIndex() {
10961096
// 246 paths
10971097
// 402 operations
10981098
// 537 component schemas
1099-
// 696 reference schemas
1100-
// 9798 inline schemas
1101-
// 711 inline schemas that are objects or arrays
1102-
// 11031 total schemas
1099+
// 1972 reference schemas
1100+
// 11749 inline schemas
1101+
// 2612 inline schemas that are objects or arrays
1102+
// 14258 total schemas
11031103
// 1516 enums
11041104
// 828 polymorphic references
11051105
}

0 commit comments

Comments
 (0)