-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathextract_refs_test.go
215 lines (206 loc) · 5.22 KB
/
extract_refs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package index
import (
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestSpecIndex_ExtractRefs_CheckDescriptionNotMap(t *testing.T) {
yml := `openapi: 3.1.0
info:
description: This is a description
paths:
/herbs/and/spice:
get:
description: This is a also a description
responses:
200:
content:
application/json:
schema:
type: array
properties:
description:
type: string
`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allDescriptions, 2)
assert.Equal(t, 2, idx.descriptionCount)
}
func TestSpecIndex_ExtractRefs_CheckSummarySummary(t *testing.T) {
yml := `things:
summary:
summary:
- summary`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allSummaries, 3)
assert.Equal(t, 3, idx.summaryCount)
}
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:
type: object
prefixItems:
- $ref: '#/components/schemas/Test'
additionalProperties: false
unevaluatedProperties: false
components:
schemas:
Test:
type: object
additionalProperties:
type: string
contains:
type: string
not:
type: number
unevaluatedProperties:
type: boolean
patternProperties:
^S_:
type: string
^I_:
type: integer
prefixItems:
- type: string
AllOf:
allOf:
- type: object
properties:
test:
type: string
- type: object
properties:
test2:
type: string
AnyOf:
anyOf:
- type: object
properties:
test:
type: string
- type: object
properties:
test2:
type: string
OneOf:
oneOf:
- type: string
- type: number
- type: boolean
`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allInlineSchemaDefinitions, 21)
assert.Len(t, idx.allInlineSchemaObjectDefinitions, 7)
}
// https://github.com/pb33f/libopenapi/issues/112
func TestSpecIndex_ExtractRefs_CheckReferencesWithBracketsInName(t *testing.T) {
yml := `openapi: 3.0.0
components:
schemas:
Cake[Burger]:
type: string
description: A cakey burger
Happy:
type: object
properties:
mingo:
$ref: '#/components/schemas/Cake[Burger]'
`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allMappedRefs, 1)
assert.Equal(t, "Cake[Burger]", idx.allMappedRefs["#/components/schemas/Cake[Burger]"].Name)
}
// https://github.com/daveshanley/vacuum/issues/339
func TestSpecIndex_ExtractRefs_CheckEnumNotPropertyCalledEnum(t *testing.T) {
yml := `openapi: 3.0.0
components:
schemas:
SimpleFieldSchema:
description: Schema of a field as described in JSON Schema draft 2019-09
type: object
required:
- type
- description
properties:
type:
type: string
enum:
- string
- number
description:
type: string
description: A description of the property
enum:
type: array
description: A array of describing the possible values
items:
type: string
example:
- yo
- hello
Schema2:
type: object
properties:
enumRef:
$ref: '#/components/schemas/enum'
enum:
type: string
enum: [big, small]
nullable: true
enum:
type: [string, null]
enum: [big, small]
`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allEnums, 3)
}
func TestSpecIndex_ExtractRefs_CheckRefsUnderExtensionsAreNotIncluded(t *testing.T) {
yml := `openapi: 3.1.0
components:
schemas:
Pasta:
x-hello:
thing:
$ref: '404'
`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
c.ExcludeExtensionRefs = true
idx := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, idx.allMappedRefs, 0)
assert.Len(t, idx.allRefs, 0)
assert.Len(t, idx.refErrors, 0)
}