-
Notifications
You must be signed in to change notification settings - Fork 38
/
schema_test.go
326 lines (271 loc) · 11.6 KB
/
schema_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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package analysis
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"path"
"path/filepath"
"testing"
"github.com/go-openapi/analysis/internal/antest"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var knownSchemas = []*spec.Schema{
spec.BoolProperty(), // 0
spec.StringProperty(), // 1
spec.Int8Property(), // 2
spec.Int16Property(), // 3
spec.Int32Property(), // 4
spec.Int64Property(), // 5
spec.Float32Property(), // 6
spec.Float64Property(), // 7
spec.DateProperty(), // 8
spec.DateTimeProperty(), // 9
(&spec.Schema{}), // 10
(&spec.Schema{}).Typed("object", ""), // 11
(&spec.Schema{}).Typed("", ""), // 12
(&spec.Schema{}).Typed("", "uuid"), // 13
}
func TestSchemaAnalysis_KnownTypes(t *testing.T) {
for i, v := range knownSchemas {
sch, err := Schema(SchemaOpts{Schema: v})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsKnownType, "item at %d should be a known type", i)
}
for i, v := range complexSchemas {
sch, err := Schema(SchemaOpts{Schema: v})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Falsef(t, sch.IsKnownType, "item at %d should not be a known type", i)
}
serv := refServer()
defer serv.Close()
for i, ref := range knownRefs(serv.URL) {
sch, err := Schema(SchemaOpts{Schema: refSchema(ref)})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsKnownType, "item at %d should be a known type", i)
}
for i, ref := range complexRefs(serv.URL) {
sch, err := Schema(SchemaOpts{Schema: refSchema(ref)})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Falsef(t, sch.IsKnownType, "item at %d should not be a known type", i)
}
}
func TestSchemaAnalysis_Array(t *testing.T) {
for i, v := range append(knownSchemas, (&spec.Schema{}).Typed("array", "")) {
sch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(v)})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsArray, "item at %d should be an array type", i)
assert.Truef(t, sch.IsSimpleArray, "item at %d should be a simple array type", i)
}
for i, v := range complexSchemas {
sch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(v)})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsArray, "item at %d should be an array type", i)
assert.Falsef(t, sch.IsSimpleArray, "item at %d should not be a simple array type", i)
}
serv := refServer()
defer serv.Close()
for i, ref := range knownRefs(serv.URL) {
sch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(refSchema(ref))})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsArray, "item at %d should be an array type", i)
assert.Truef(t, sch.IsSimpleArray, "item at %d should be a simple array type", i)
}
for i, ref := range complexRefs(serv.URL) {
sch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(refSchema(ref))})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Falsef(t, sch.IsKnownType, "item at %d should not be a known type", i)
assert.Truef(t, sch.IsArray, "item at %d should be an array type", i)
assert.Falsef(t, sch.IsSimpleArray, "item at %d should not be a simple array type", i)
}
// edge case: unrestricted array (beyond Swagger)
at := spec.ArrayProperty(nil)
at.Items = nil
sch, err := Schema(SchemaOpts{Schema: at})
require.NoError(t, err)
assert.True(t, sch.IsArray)
assert.False(t, sch.IsTuple)
assert.False(t, sch.IsKnownType)
assert.True(t, sch.IsSimpleSchema)
// unrestricted array with explicit empty schema
at = spec.ArrayProperty(nil)
at.Items = &spec.SchemaOrArray{}
sch, err = Schema(SchemaOpts{Schema: at})
require.NoError(t, err)
assert.True(t, sch.IsArray)
assert.False(t, sch.IsTuple)
assert.False(t, sch.IsKnownType)
assert.True(t, sch.IsSimpleSchema)
}
func TestSchemaAnalysis_Map(t *testing.T) {
for i, v := range append(knownSchemas, spec.MapProperty(nil)) {
sch, err := Schema(SchemaOpts{Schema: spec.MapProperty(v)})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsMap, "item at %d should be a map type", i)
assert.Truef(t, sch.IsSimpleMap, "item at %d should be a simple map type", i)
}
for i, v := range complexSchemas {
sch, err := Schema(SchemaOpts{Schema: spec.MapProperty(v)})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsMap, "item at %d should be a map type", i)
assert.Falsef(t, sch.IsSimpleMap, "item at %d should not be a simple map type", i)
}
}
func TestSchemaAnalysis_ExtendedObject(t *testing.T) {
for i, v := range knownSchemas {
wex := spec.MapProperty(v).SetProperty("name", *spec.StringProperty())
sch, err := Schema(SchemaOpts{Schema: wex})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsExtendedObject, "item at %d should be an extended map object type", i)
assert.Falsef(t, sch.IsMap, "item at %d should not be a map type", i)
assert.Falsef(t, sch.IsSimpleMap, "item at %d should not be a simple map type", i)
}
}
func TestSchemaAnalysis_Tuple(t *testing.T) {
at := spec.ArrayProperty(nil)
at.Items = &spec.SchemaOrArray{}
at.Items.Schemas = append(at.Items.Schemas, *spec.StringProperty(), *spec.Int64Property())
sch, err := Schema(SchemaOpts{Schema: at})
require.NoError(t, err)
assert.True(t, sch.IsTuple)
assert.False(t, sch.IsTupleWithExtra)
assert.False(t, sch.IsKnownType)
assert.False(t, sch.IsSimpleSchema)
// edge case: tuple with a single element
at.Items = &spec.SchemaOrArray{}
at.Items.Schemas = append(at.Items.Schemas, *spec.StringProperty())
sch, err = Schema(SchemaOpts{Schema: at})
require.NoError(t, err)
assert.True(t, sch.IsTuple)
assert.False(t, sch.IsTupleWithExtra)
assert.False(t, sch.IsKnownType)
assert.False(t, sch.IsSimpleSchema)
}
func TestSchemaAnalysis_TupleWithExtra(t *testing.T) {
at := spec.ArrayProperty(nil)
at.Items = &spec.SchemaOrArray{}
at.Items.Schemas = append(at.Items.Schemas, *spec.StringProperty(), *spec.Int64Property())
at.AdditionalItems = &spec.SchemaOrBool{Allows: true}
at.AdditionalItems.Schema = spec.Int32Property()
sch, err := Schema(SchemaOpts{Schema: at})
require.NoError(t, err)
assert.False(t, sch.IsTuple)
assert.True(t, sch.IsTupleWithExtra)
assert.False(t, sch.IsKnownType)
assert.False(t, sch.IsSimpleSchema)
}
func TestSchemaAnalysis_BaseType(t *testing.T) {
cl := (&spec.Schema{}).Typed("object", "").SetProperty("type", *spec.StringProperty()).WithDiscriminator("type")
sch, err := Schema(SchemaOpts{Schema: cl})
require.NoError(t, err)
assert.True(t, sch.IsBaseType)
assert.False(t, sch.IsKnownType)
assert.False(t, sch.IsSimpleSchema)
}
func TestSchemaAnalysis_SimpleSchema(t *testing.T) {
for i, v := range append(knownSchemas, spec.ArrayProperty(nil), spec.MapProperty(nil)) {
sch, err := Schema(SchemaOpts{Schema: v})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Truef(t, sch.IsSimpleSchema, "item at %d should be a simple schema", i)
asch, err := Schema(SchemaOpts{Schema: spec.ArrayProperty(v)})
require.NoErrorf(t, err, "failed to analyze array schema at %d: %v", i, err)
assert.Truef(t, asch.IsSimpleSchema, "array item at %d should be a simple schema", i)
msch, err := Schema(SchemaOpts{Schema: spec.MapProperty(v)})
require.NoErrorf(t, err, "failed to analyze map schema at %d: %v", i, err)
assert.Truef(t, msch.IsSimpleSchema, "map item at %d should be a simple schema", i)
}
for i, v := range complexSchemas {
sch, err := Schema(SchemaOpts{Schema: v})
require.NoErrorf(t, err, "failed to analyze schema at %d: %v", i, err)
assert.Falsef(t, sch.IsSimpleSchema, "item at %d should not be a simple schema", i)
}
}
func TestSchemaAnalys_InvalidSchema(t *testing.T) {
// explore error cases in schema analysis:
// the only cause for failure is a wrong $ref at some place
bp := filepath.Join("fixtures", "bugs", "1602", "other-invalid-pointers.yaml")
sp := antest.LoadOrFail(t, bp)
// invalid ref not detected (no digging further)
def := sp.Definitions["invalidRefInObject"]
_, err := Schema(SchemaOpts{Schema: &def, Root: sp, BasePath: bp})
require.NoError(t, err, "did not expect an error here, in spite of the underlying invalid $ref")
def = sp.Definitions["invalidRefInTuple"]
_, err = Schema(SchemaOpts{Schema: &def, Root: sp, BasePath: bp})
require.NoError(t, err, "did not expect an error here, in spite of the underlying invalid $ref")
// invalid ref detected (digging)
schema := refSchema(spec.MustCreateRef("#/definitions/noWhere"))
_, err = Schema(SchemaOpts{Schema: schema, Root: sp, BasePath: bp})
require.Error(t, err, "expected an error here")
def = sp.Definitions["invalidRefInMap"]
_, err = Schema(SchemaOpts{Schema: &def, Root: sp, BasePath: bp})
require.Error(t, err, "expected an error here")
def = sp.Definitions["invalidRefInArray"]
_, err = Schema(SchemaOpts{Schema: &def, Root: sp, BasePath: bp})
require.Error(t, err, "expected an error here")
def = sp.Definitions["indirectToInvalidRef"]
_, err = Schema(SchemaOpts{Schema: &def, Root: sp, BasePath: bp})
require.Error(t, err, "expected an error here")
}
func TestSchemaAnalysis_EdgeCases(t *testing.T) {
t.Parallel()
_, err := Schema(SchemaOpts{Schema: nil})
require.Error(t, err)
}
/* helpers for the Schema test suite */
func newCObj() *spec.Schema {
return (&spec.Schema{}).Typed("object", "").SetProperty("id", *spec.Int64Property())
}
var complexObject = newCObj()
var complexSchemas = []*spec.Schema{
complexObject,
spec.ArrayProperty(complexObject),
spec.MapProperty(complexObject),
}
func knownRefs(base string) []spec.Ref {
urls := []string{"bool", "string", "integer", "float", "date", "object", "format"}
result := make([]spec.Ref, 0, len(urls))
for _, u := range urls {
result = append(result, spec.MustCreateRef(fmt.Sprintf("%s/%s", base, path.Join("known", u))))
}
return result
}
func complexRefs(base string) []spec.Ref {
urls := []string{"object", "array", "map"}
result := make([]spec.Ref, 0, len(urls))
for _, u := range urls {
result = append(result, spec.MustCreateRef(fmt.Sprintf("%s/%s", base, path.Join("complex", u))))
}
return result
}
func refServer() *httptest.Server {
mux := http.NewServeMux()
mux.Handle("/known/bool", schemaHandler(knownSchemas[0]))
mux.Handle("/known/string", schemaHandler(knownSchemas[1]))
mux.Handle("/known/integer", schemaHandler(knownSchemas[5]))
mux.Handle("/known/float", schemaHandler(knownSchemas[6]))
mux.Handle("/known/date", schemaHandler(knownSchemas[8]))
mux.Handle("/known/object", schemaHandler(knownSchemas[11]))
mux.Handle("/known/format", schemaHandler(knownSchemas[13]))
mux.Handle("/complex/object", schemaHandler(complexSchemas[0]))
mux.Handle("/complex/array", schemaHandler(complexSchemas[1]))
mux.Handle("/complex/map", schemaHandler(complexSchemas[2]))
return httptest.NewServer(mux)
}
func refSchema(ref spec.Ref) *spec.Schema {
return &spec.Schema{SchemaProps: spec.SchemaProps{Ref: ref}}
}
func schemaHandler(schema *spec.Schema) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, schema)
})
}
func writeJSON(w http.ResponseWriter, data interface{}) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
if err := enc.Encode(data); err != nil {
panic(err)
}
}