forked from pb33f/libopenapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_iteration_test.go
303 lines (238 loc) · 6.63 KB
/
document_iteration_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package libopenapi
import (
"os"
"slices"
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/require"
)
type loopFrame struct {
Type string
Restricted bool
}
type context struct {
visited []string
stack []loopFrame
}
func Test_Speakeasy_Document_Iteration(t *testing.T) {
spec, err := os.ReadFile("test_specs/speakeasy-test.yaml")
require.NoError(t, err)
doc, err := NewDocumentWithConfiguration(spec, &datamodel.DocumentConfiguration{
BasePath: "./test_specs",
IgnorePolymorphicCircularReferences: true,
IgnoreArrayCircularReferences: true,
AllowFileReferences: true,
})
require.NoError(t, err)
m, errs := doc.BuildV3Model()
require.Empty(t, errs)
for path, pathItem := range m.Model.Paths.PathItems.FromOldest() {
t.Log(path)
iterateOperations(t, pathItem.GetOperations())
}
for path, pathItem := range m.Model.Webhooks.FromOldest() {
t.Log(path)
iterateOperations(t, pathItem.GetOperations())
}
for name, schemaProxy := range m.Model.Components.Schemas.FromOldest() {
t.Log(name)
handleSchema(t, schemaProxy, context{})
}
}
func iterateOperations(t *testing.T, ops *orderedmap.Map[string, *v3.Operation]) {
for method, op := range ops.FromOldest() {
t.Log(method)
for i, param := range op.Parameters {
t.Log("param", i, param.Name)
if param.Schema != nil {
handleSchema(t, param.Schema, context{})
}
}
if op.RequestBody != nil {
t.Log("request body")
for contentType, mediaType := range op.RequestBody.Content.FromOldest() {
t.Log(contentType)
if mediaType.Schema != nil {
handleSchema(t, mediaType.Schema, context{})
}
}
}
if orderedmap.Len(op.Responses.Codes) > 0 {
t.Log("responses")
}
for code, response := range op.Responses.Codes.FromOldest() {
t.Log(code)
for contentType, mediaType := range response.Content.FromOldest() {
t.Log(contentType)
if mediaType.Schema != nil {
handleSchema(t, mediaType.Schema, context{})
}
}
}
if orderedmap.Len(op.Responses.Codes) > 0 {
t.Log("callbacks")
}
for callbackName, callback := range op.Callbacks.FromOldest() {
t.Log(callbackName)
for name, pathItem := range callback.Expression.FromOldest() {
t.Log(name)
iterateOperations(t, pathItem.GetOperations())
}
}
}
}
func handleSchema(t *testing.T, schProxy *base.SchemaProxy, ctx context) {
if checkCircularReference(t, &ctx, schProxy) {
return
}
sch, err := schProxy.BuildSchema()
require.NoError(t, err)
typ, subTypes := getResolvedType(sch)
t.Log("schema", typ, subTypes)
if len(sch.Enum) > 0 {
switch typ {
case "string":
return
case "integer":
return
default:
// handle as base type
}
}
switch typ {
case "allOf":
fallthrough
case "anyOf":
fallthrough
case "oneOf":
if len(subTypes) > 0 {
return
}
handleAllOfAnyOfOneOf(t, sch, ctx)
case "array":
handleArray(t, sch, ctx)
case "object":
handleObject(t, sch, ctx)
default:
return
}
}
func getResolvedType(sch *base.Schema) (string, []string) {
subTypes := []string{}
for _, t := range sch.Type {
if t == "" { // treat empty type as any
subTypes = append(subTypes, "any")
} else if t != "null" {
subTypes = append(subTypes, t)
}
}
if len(sch.AllOf) > 0 {
return "allOf", nil
}
if len(sch.AnyOf) > 0 {
return "anyOf", nil
}
if len(sch.OneOf) > 0 {
return "oneOf", nil
}
if len(subTypes) == 0 {
if len(sch.Enum) > 0 {
return "string", nil
}
if orderedmap.Len(sch.Properties) > 0 {
return "object", nil
}
if sch.AdditionalProperties != nil {
return "object", nil
}
if sch.Items != nil {
return "array", nil
}
return "any", nil
}
if len(subTypes) == 1 {
return subTypes[0], nil
}
return "oneOf", subTypes
}
func handleAllOfAnyOfOneOf(t *testing.T, sch *base.Schema, ctx context) {
var schemas []*base.SchemaProxy
switch {
case len(sch.AllOf) > 0:
schemas = sch.AllOf
case len(sch.AnyOf) > 0:
schemas = sch.AnyOf
ctx.stack = append(ctx.stack, loopFrame{Type: "anyOf", Restricted: len(sch.AnyOf) == 1})
case len(sch.OneOf) > 0:
schemas = sch.OneOf
ctx.stack = append(ctx.stack, loopFrame{Type: "oneOf", Restricted: len(sch.OneOf) == 1})
}
for _, s := range schemas {
handleSchema(t, s, ctx)
}
}
func handleArray(t *testing.T, sch *base.Schema, ctx context) {
ctx.stack = append(ctx.stack, loopFrame{Type: "array", Restricted: sch.MinItems != nil && *sch.MinItems > 0})
if sch.Items != nil && sch.Items.IsA() {
handleSchema(t, sch.Items.A, ctx)
}
if sch.Contains != nil {
handleSchema(t, sch.Contains, ctx)
}
if sch.PrefixItems != nil {
for _, s := range sch.PrefixItems {
handleSchema(t, s, ctx)
}
}
}
func handleObject(t *testing.T, sch *base.Schema, ctx context) {
for name, schemaProxy := range sch.Properties.FromOldest() {
ctx.stack = append(ctx.stack, loopFrame{Type: "object", Restricted: slices.Contains(sch.Required, name)})
handleSchema(t, schemaProxy, ctx)
}
if sch.AdditionalProperties != nil && sch.AdditionalProperties.IsA() {
handleSchema(t, sch.AdditionalProperties.A, ctx)
}
}
func checkCircularReference(t *testing.T, ctx *context, schProxy *base.SchemaProxy) bool {
loopRef := getSimplifiedRef(schProxy.GetReference())
if loopRef != "" {
if slices.Contains(ctx.visited, loopRef) {
isRestricted := true
containsObject := false
for _, v := range ctx.stack {
if v.Type == "object" {
containsObject = true
}
if v.Type == "array" && !v.Restricted {
isRestricted = false
} else if !v.Restricted {
isRestricted = false
}
}
if !containsObject {
isRestricted = true
}
require.False(t, isRestricted, "circular reference: %s", append(ctx.visited, loopRef))
return true
}
ctx.visited = append(ctx.visited, loopRef)
}
return false
}
// getSimplifiedRef will return the reference without the preceding file path
// caveat is that if a spec has the same ref in two different files they include this may identify them incorrectly
// but currently a problem anyway as libopenapi when returning references from an external file won't include the file path
// for a local reference with that file and so we might fail to distinguish between them that way.
// The fix needed is for libopenapi to also track which file the reference is in so we can always prefix them with the file path
func getSimplifiedRef(ref string) string {
if ref == "" {
return ""
}
refParts := strings.Split(ref, "#/")
return "#/" + refParts[len(refParts)-1]
}