@@ -4,22 +4,17 @@ import (
4
4
"embed"
5
5
"errors"
6
6
"fmt"
7
- "github.com/TykTechnologies/tyk/pkg/utils"
8
7
"path/filepath"
9
- "slices"
10
8
"sort"
11
9
"strings"
12
10
"sync"
13
11
14
- "github.com/buger/jsonparser"
15
- "github.com/hashicorp/go-multierror"
16
- pkgver "github.com/hashicorp/go-version"
17
- lru "github.com/hashicorp/golang-lru"
18
-
19
- "github.com/TykTechnologies/tyk/common/option"
20
12
tykerrors "github.com/TykTechnologies/tyk/internal/errors"
21
13
"github.com/TykTechnologies/tyk/internal/service/gojsonschema"
22
14
logger "github.com/TykTechnologies/tyk/log"
15
+ "github.com/buger/jsonparser"
16
+ "github.com/hashicorp/go-multierror"
17
+ pkgver "github.com/hashicorp/go-version"
23
18
)
24
19
25
20
//go:embed schema/*
@@ -31,12 +26,6 @@ const (
31
26
keyRequired = "required"
32
27
keyAnyOf = "anyOf"
33
28
oasSchemaVersionNotFoundFmt = "Schema not found for version %q"
34
-
35
- defaultLruCacheSize = 1 << 10
36
- )
37
-
38
- var (
39
- errUnexpected = errors .New ("unexpected: invariant broken" )
40
29
)
41
30
42
31
var (
47
36
oasJSONSchemas map [string ][]byte
48
37
49
38
defaultVersion string
50
-
51
- defaultSchemaCache = utils .Must (NewLruSchemaCache (defaultLruCacheSize ))
52
39
)
53
40
54
41
func loadOASSchema () error {
@@ -148,27 +135,6 @@ func ValidateOASObject(documentBody []byte, oasVersion string) error {
148
135
return validateJSON (oasSchema , documentBody )
149
136
}
150
137
151
- // ValidateOASObjectWithOptions validates an OAS documents due to schema with additional options.
152
- func ValidateOASObjectWithOptions (documentBody []byte , oasVersion string , opts ... option.Option [schemaModifier ]) error {
153
- oasSchema , err := GetOASSchema (oasVersion )
154
- if err != nil {
155
- return err
156
- }
157
-
158
- modifier := option .New (opts ).Build (schemaModifier {
159
- cache : defaultSchemaCache ,
160
- version : oasVersion ,
161
- schema : oasSchema ,
162
- })
163
-
164
- oasSchema , err = modifier .getSchema ()
165
- if err != nil {
166
- return err
167
- }
168
-
169
- return validateJSON (oasSchema , documentBody )
170
- }
171
-
172
138
// ValidateOASTemplate checks a Tyk OAS API template for necessary fields,
173
139
// acknowledging that some standard Tyk OAS API fields are optional in templates.
174
140
func ValidateOASTemplate (documentBody []byte , oasVersion string ) error {
@@ -235,20 +201,6 @@ func GetOASSchema(version string) ([]byte, error) {
235
201
return oasSchema , nil
236
202
}
237
203
238
- // AllowRootFields extends list of allowed fields on fly if field is not described
239
- func AllowRootFields (fields ... string ) option.Option [schemaModifier ] {
240
- return func (s * schemaModifier ) {
241
- s .allowedFields = fields
242
- }
243
- }
244
-
245
- // WithCache allows pass optional cache
246
- func WithCache (cache SchemaCache ) option.Option [schemaModifier ] {
247
- return func (s * schemaModifier ) {
248
- s .cache = cache
249
- }
250
- }
251
-
252
204
func findDefaultVersion (rawVersions []string ) string {
253
205
versions := make ([]* pkgver.Version , len (rawVersions ))
254
206
for i , raw := range rawVersions {
@@ -280,115 +232,3 @@ func getMinorVersion(version string) (string, error) {
280
232
segments := v .Segments ()
281
233
return fmt .Sprintf ("%d.%d" , segments [0 ], segments [1 ]), nil
282
234
}
283
-
284
- type schemaModifier struct {
285
- version string
286
- schema []byte
287
- allowedFields []string
288
- cache SchemaCache
289
- }
290
-
291
- func (c * schemaModifier ) cacheKey () string {
292
- allowedFields := slices .Clone (c .allowedFields )
293
- slices .SortFunc (allowedFields , strings .Compare )
294
- return c .version + ":" + strings .Join (allowedFields , ":" )
295
- }
296
-
297
- func (c * schemaModifier ) getSchema () ([]byte , error ) {
298
- if len (c .allowedFields ) == 0 {
299
- return c .schema , nil
300
- }
301
-
302
- key := c .cacheKey ()
303
-
304
- if schema , err := c .cache .Get (key , func () ([]byte , error ) {
305
- var schema = copyBytes (c .schema )
306
-
307
- for _ , field := range c .allowedFields {
308
- _ , _ , _ , err := jsonparser .Get (schema , keyProperties , field )
309
-
310
- if errors .Is (err , jsonparser .KeyPathNotFoundError ) {
311
- if schema , err = jsonparser .Set (schema , []byte (`{}` ), keyProperties , field ); err != nil {
312
- return nil , err
313
- }
314
- } else if err != nil {
315
- return nil , err
316
- }
317
- }
318
-
319
- return schema , nil
320
- }); err != nil {
321
- return nil , err
322
- } else {
323
- return schema , nil
324
- }
325
- }
326
-
327
- // SchemaCache json schema cache.
328
- type SchemaCache interface {
329
- Get (key string , create func () ([]byte , error )) ([]byte , error )
330
- }
331
-
332
- // interface created for acceptance tests purposes
333
- type extendedSchemaCache interface {
334
- SchemaCache
335
- Purge ()
336
- Len () int
337
- }
338
-
339
- var _ SchemaCache = (* LruSchemaCache )(nil )
340
-
341
- // LruSchemaCache schema caching object which implements LRU algorithm under the hood.
342
- type LruSchemaCache struct {
343
- capacity int
344
- cache * lru.Cache
345
- }
346
-
347
- // NewLruSchemaCache create new cache instance.
348
- func NewLruSchemaCache (capacity int ) (* LruSchemaCache , error ) {
349
- lruCache , err := lru .New (capacity )
350
-
351
- if err != nil {
352
- return nil , err
353
- }
354
-
355
- return & LruSchemaCache {
356
- capacity : capacity ,
357
- cache : lruCache ,
358
- }, nil
359
- }
360
-
361
- func (l * LruSchemaCache ) Get (key string , create func () ([]byte , error )) ([]byte , error ) {
362
- if res , ok := l .cache .Get (key ); ok {
363
- bRes , okCast := res .([]byte )
364
-
365
- if ! okCast {
366
- return nil , errUnexpected
367
- }
368
-
369
- return bRes , nil
370
- }
371
-
372
- newEntry , err := create ()
373
- if err != nil {
374
- return nil , err
375
- }
376
-
377
- _ = l .cache .Add (key , newEntry )
378
-
379
- return newEntry , nil
380
- }
381
-
382
- func (l * LruSchemaCache ) Purge () {
383
- l .cache .Purge ()
384
- }
385
-
386
- func (l * LruSchemaCache ) Len () int {
387
- return l .cache .Len ()
388
- }
389
-
390
- func copyBytes (in []byte ) []byte {
391
- out := make ([]byte , len (in ))
392
- copy (out , in )
393
- return out
394
- }
0 commit comments