...
1
16
17 package testing
18
19 import (
20 "os"
21 "path/filepath"
22 "sync"
23
24 openapi_v3 "github.com/google/gnostic-models/openapiv3"
25 )
26
27 type FakeV3 struct {
28 Path string
29
30 lock sync.Mutex
31 documents map[string]*openapi_v3.Document
32 errors map[string]error
33 }
34
35 func (f *FakeV3) OpenAPIV3Schema(groupVersion string) (*openapi_v3.Document, error) {
36 f.lock.Lock()
37 defer f.lock.Unlock()
38
39 if existing, ok := f.documents[groupVersion]; ok {
40 return existing, nil
41 } else if existingError, ok := f.errors[groupVersion]; ok {
42 return nil, existingError
43 }
44
45 _, err := os.Stat(f.Path)
46 if err != nil {
47 return nil, err
48 }
49 spec, err := os.ReadFile(filepath.Join(f.Path, groupVersion+".json"))
50 if err != nil {
51 return nil, err
52 }
53
54 if f.documents == nil {
55 f.documents = make(map[string]*openapi_v3.Document)
56 }
57
58 if f.errors == nil {
59 f.errors = make(map[string]error)
60 }
61
62 result, err := openapi_v3.ParseDocument(spec)
63 if err != nil {
64 f.errors[groupVersion] = err
65 return nil, err
66 }
67
68 f.documents[groupVersion] = result
69 return result, nil
70 }
71
View as plain text