-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathschema_proxy_test.go
More file actions
280 lines (221 loc) · 7.61 KB
/
Copy pathschema_proxy_test.go
File metadata and controls
280 lines (221 loc) · 7.61 KB
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
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package base
import (
"context"
"log/slog"
"os"
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestSchemaProxy_Build(t *testing.T) {
yml := `x-windows: washed
description: something`
var sch SchemaProxy
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
ctx := context.WithValue(context.Background(), "key", "value")
err := sch.Build(ctx, &idxNode, idxNode.Content[0], nil)
assert.NoError(t, err)
assert.Equal(t, "value", sch.GetContext().Value("key"))
assert.Equal(t, "e20c009d370944d177c0b46e8fa29e15fadc3a6f9cca6bb251ff9e120265fc96",
low.GenerateHashString(&sch))
assert.Equal(t, "something", sch.Schema().Description.GetValue())
assert.Empty(t, sch.GetReference())
assert.NotNil(t, sch.GetKeyNode())
assert.NotNil(t, sch.GetValueNode())
assert.False(t, sch.IsReference())
sch.SetReference("coffee", nil)
assert.Equal(t, "coffee", sch.GetReference())
// already rendered, should spit out the same
assert.Equal(t, "37290d74ac4d186e3a8e5785d259d2ec04fac91ae28092e7620ec8bc99e830aa",
low.GenerateHashString(&sch))
assert.Equal(t, 1, orderedmap.Len(sch.Schema().GetExtensions()))
assert.Nil(t, sch.GetIndex())
}
func TestSchemaProxy_Build_CheckRef(t *testing.T) {
yml := `$ref: wat`
var sch SchemaProxy
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
err := sch.Build(context.Background(), nil, idxNode.Content[0], nil)
assert.NoError(t, err)
assert.True(t, sch.IsReference())
assert.Equal(t, "wat", sch.GetReference())
assert.Equal(t, "f00a787f7492a95e165b470702f4fe9373583fbdc025b2c8bdf0262cc48fcff4",
low.GenerateHashString(&sch))
}
func TestSchemaProxy_Build_HashInline(t *testing.T) {
yml := `type: int`
var sch SchemaProxy
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
err := sch.Build(context.Background(), nil, idxNode.Content[0], nil)
assert.NoError(t, err)
assert.False(t, sch.IsReference())
assert.NotNil(t, sch.Schema())
assert.Equal(t, "6da88c34ba124c41f977db66a4fc5c1a951708d285c81bb0d47c3206f4c27ca8",
low.GenerateHashString(&sch))
}
func TestSchemaProxy_Build_UsingMergeNodes(t *testing.T) {
yml := `
x-common-definitions:
life_cycle_types: &life_cycle_types_def
type: string
enum: ["Onboarding", "Monitoring", "Re-Assessment"]
description: The type of life cycle
<<: *life_cycle_types_def`
var sch SchemaProxy
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
err := sch.Build(context.Background(), nil, idxNode.Content[0], nil)
assert.NoError(t, err)
assert.Len(t, sch.Schema().Enum.Value, 3)
assert.Equal(t, "The type of life cycle", sch.Schema().Description.Value)
}
func TestSchemaProxy_GetSchemaReferenceLocation(t *testing.T) {
yml := `type: object
properties:
name:
type: string
description: thing`
var idxNodeA yaml.Node
e := yaml.Unmarshal([]byte(yml), &idxNodeA)
assert.NoError(t, e)
yml = `
type: object
properties:
name:
type: string
description: thang`
var schA SchemaProxy
var schB SchemaProxy
var schC SchemaProxy
var idxNodeB yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNodeB)
c := index.CreateOpenAPIIndexConfig()
rolo := index.NewRolodex(c)
rolo.SetRootNode(&idxNodeA)
_ = rolo.IndexTheRolodex(context.Background())
err := schA.Build(context.Background(), nil, idxNodeA.Content[0], rolo.GetRootIndex())
assert.NoError(t, err)
err = schB.Build(context.Background(), nil, idxNodeB.Content[0].Content[3].Content[1], rolo.GetRootIndex())
assert.NoError(t, err)
rolo.GetRootIndex().SetAbsolutePath("/rooty/rootster")
origin := schA.GetSchemaReferenceLocation()
assert.NotNil(t, origin)
assert.Equal(t, "/rooty/rootster", origin.AbsoluteLocation)
// mess things up so it cannot be found
schA.vn = schB.vn
origin = schA.GetSchemaReferenceLocation()
assert.Nil(t, origin)
// create a new index
idx := index.NewSpecIndexWithConfig(&idxNodeB, c)
idx.SetAbsolutePath("/boaty/mcboatface")
// add the index to the rolodex
rolo.AddIndex(idx)
// can now find the origin
origin = schA.GetSchemaReferenceLocation()
assert.NotNil(t, origin)
assert.Equal(t, "/boaty/mcboatface", origin.AbsoluteLocation)
// do it again, but with no index
err = schC.Build(context.Background(), nil, idxNodeA.Content[0], nil)
assert.NoError(t, err)
origin = schC.GetSchemaReferenceLocation()
assert.Nil(t, origin)
}
func TestSchemaProxy_Build_HashFail(t *testing.T) {
sp := new(SchemaProxy)
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
idx := index.NewSpecIndexWithConfig(nil, &index.SpecIndexConfig{Logger: logger})
sp.idx = idx
v := sp.Hash()
assert.Equal(t, [32]byte{}, v)
}
func TestSchemaProxy_AddNodePassthrough(t *testing.T) {
yml := `type: int
description: cakes`
sch := SchemaProxy{}
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
err := sch.Build(context.Background(), nil, idxNode.Content[0], nil)
assert.NoError(t, err)
n, f := sch.Nodes.Load(3)
assert.False(t, f)
assert.Nil(t, n)
sch.AddNode(3, &yaml.Node{Value: "3"})
s := sch.Schema()
sch.AddNode(4, &yaml.Node{Value: "4"})
n, f = s.Nodes.Load(3)
assert.True(t, f)
assert.NotNil(t, n)
n, f = s.Nodes.Load(4)
assert.True(t, f)
assert.NotNil(t, n)
}
func TestSchemaProxy_QuickHash_Empty(t *testing.T) {
sp := new(SchemaProxy)
r := low.Reference{}
r.SetReference("hello", &yaml.Node{})
sp.Reference = r
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
cfg := &index.SpecIndexConfig{Logger: logger, UseSchemaQuickHash: true}
idx := index.NewSpecIndexWithConfig(nil, cfg)
sp.idx = idx
rolo := index.NewRolodex(cfg)
idx.SetRolodex(rolo)
rolo.SetRootIndex(idx)
v := sp.Hash()
assert.Equal(t, [32]byte{}, v)
}
func TestSchemaProxy_TestRolodexHasId(t *testing.T) {
yml := `type: int`
var sch SchemaProxy
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndexWithConfig(idxNode.Content[0], &index.SpecIndexConfig{})
rolo := index.NewRolodex(&index.SpecIndexConfig{})
rolo.SetRootIndex(idx)
idx.SetRolodex(rolo)
err := sch.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.NoError(t, err)
assert.False(t, sch.IsReference())
assert.NotNil(t, sch.Schema())
assert.Equal(t, "6da88c34ba124c41f977db66a4fc5c1a951708d285c81bb0d47c3206f4c27ca8",
low.GenerateHashString(&sch))
}
func TestSchemaProxy_Hash_UseSchemaQuickHash_NonCircular(t *testing.T) {
yml := `type: object
properties:
name:
type: string
age:
type: integer`
var sch SchemaProxy
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
// Create index with UseSchemaQuickHash enabled
cfg := &index.SpecIndexConfig{UseSchemaQuickHash: true}
idx := index.NewSpecIndexWithConfig(idxNode.Content[0], cfg)
rolo := index.NewRolodex(cfg)
rolo.SetRootIndex(idx)
idx.SetRolodex(rolo)
err := sch.Build(context.Background(), nil, idxNode.Content[0], idx)
assert.NoError(t, err)
// Ensure this is not a reference schema (to trigger the !sp.IsReference() path)
assert.False(t, sch.IsReference())
// Pre-render the schema to ensure it's available
schema := sch.Schema()
assert.NotNil(t, schema)
// This should trigger lines 162-164: UseSchemaQuickHash is true,
// CheckSchemaProxyForCircularRefs returns false (no circular refs in simple object)
hash := sch.Hash()
// Verify we get a valid hash (not empty)
assert.NotEqual(t, [32]byte{}, hash)
// Verify the schema was rendered and available
assert.NotNil(t, sch.rendered)
}