-
-
Notifications
You must be signed in to change notification settings - Fork 432
/
loader.go
1047 lines (955 loc) · 28.6 KB
/
loader.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package openapi3
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"path/filepath"
"reflect"
"strconv"
"strings"
"github.com/ghodss/yaml"
)
func foundUnresolvedRef(ref string) error {
return fmt.Errorf("found unresolved ref: %q", ref)
}
func failedToResolveRefFragmentPart(value, what string) error {
return fmt.Errorf("failed to resolve %q in fragment in URI: %q", what, value)
}
// Loader helps deserialize an OpenAPIv3 document
type Loader struct {
// IsExternalRefsAllowed enables visiting other files
IsExternalRefsAllowed bool
// ReadFromURIFunc allows overriding the any file/URL reading func
ReadFromURIFunc func(loader *Loader, url *url.URL) ([]byte, error)
Context context.Context
rootDir string
visitedPathItemRefs map[string]struct{}
visitedDocuments map[string]*T
visitedExample map[*Example]struct{}
visitedHeader map[*Header]struct{}
visitedLink map[*Link]struct{}
visitedParameter map[*Parameter]struct{}
visitedRequestBody map[*RequestBody]struct{}
visitedResponse map[*Response]struct{}
visitedSchema map[*Schema]struct{}
visitedSecurityScheme map[*SecurityScheme]struct{}
}
// NewLoader returns an empty Loader
func NewLoader() *Loader {
return &Loader{}
}
func (loader *Loader) resetVisitedPathItemRefs() {
loader.visitedPathItemRefs = make(map[string]struct{})
}
// LoadFromURI loads a spec from a remote URL
func (loader *Loader) LoadFromURI(location *url.URL) (*T, error) {
loader.resetVisitedPathItemRefs()
return loader.loadFromURIInternal(location)
}
// LoadFromFile loads a spec from a local file path
func (loader *Loader) LoadFromFile(location string) (*T, error) {
loader.rootDir = path.Dir(location)
return loader.LoadFromURI(&url.URL{Path: filepath.ToSlash(location)})
}
func (loader *Loader) loadFromURIInternal(location *url.URL) (*T, error) {
data, err := loader.readURL(location)
if err != nil {
return nil, err
}
return loader.loadFromDataWithPathInternal(data, location)
}
func (loader *Loader) allowsExternalRefs(ref string) (err error) {
if !loader.IsExternalRefsAllowed {
err = fmt.Errorf("encountered disallowed external reference: %q", ref)
}
return
}
// loadSingleElementFromURI reads the data from ref and unmarshals to the passed element.
func (loader *Loader) loadSingleElementFromURI(ref string, rootPath *url.URL, element interface{}) (*url.URL, error) {
if err := loader.allowsExternalRefs(ref); err != nil {
return nil, err
}
parsedURL, err := url.Parse(ref)
if err != nil {
return nil, err
}
if fragment := parsedURL.Fragment; fragment != "" {
return nil, fmt.Errorf("unexpected ref fragment %q", fragment)
}
resolvedPath, err := resolvePath(rootPath, parsedURL)
if err != nil {
return nil, fmt.Errorf("could not resolve path: %v", err)
}
data, err := loader.readURL(resolvedPath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(data, element); err != nil {
return nil, err
}
return resolvedPath, nil
}
func (loader *Loader) readURL(location *url.URL) ([]byte, error) {
if f := loader.ReadFromURIFunc; f != nil {
return f(loader, location)
}
if location.Scheme != "" && location.Host != "" {
resp, err := http.Get(location.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode > 399 {
return nil, fmt.Errorf("error loading %q: request returned status code %d", location.String(), resp.StatusCode)
}
return ioutil.ReadAll(resp.Body)
}
if location.Scheme != "" || location.Host != "" || location.RawQuery != "" {
return nil, fmt.Errorf("unsupported URI: %q", location.String())
}
return ioutil.ReadFile(location.Path)
}
// LoadFromData loads a spec from a byte array
func (loader *Loader) LoadFromData(data []byte) (*T, error) {
loader.resetVisitedPathItemRefs()
doc := &T{}
if err := yaml.Unmarshal(data, doc); err != nil {
return nil, err
}
if err := loader.ResolveRefsIn(doc, nil); err != nil {
return nil, err
}
return doc, nil
}
// LoadFromDataWithPath takes the OpenAPI document data in bytes and a path where the resolver can find referred
// elements and returns a *T with all resolved data or an error if unable to load data or resolve refs.
func (loader *Loader) LoadFromDataWithPath(data []byte, location *url.URL) (*T, error) {
loader.resetVisitedPathItemRefs()
return loader.loadFromDataWithPathInternal(data, location)
}
func (loader *Loader) loadFromDataWithPathInternal(data []byte, location *url.URL) (*T, error) {
if loader.visitedDocuments == nil {
loader.visitedDocuments = make(map[string]*T)
}
uri := location.String()
if doc, ok := loader.visitedDocuments[uri]; ok {
return doc, nil
}
doc := &T{}
loader.visitedDocuments[uri] = doc
if err := yaml.Unmarshal(data, doc); err != nil {
return nil, err
}
if err := loader.ResolveRefsIn(doc, location); err != nil {
return nil, err
}
return doc, nil
}
// ResolveRefsIn expands references if for instance spec was just unmarshalled
func (loader *Loader) ResolveRefsIn(doc *T, location *url.URL) (err error) {
if loader.visitedPathItemRefs == nil {
loader.resetVisitedPathItemRefs()
}
// Visit all components
components := doc.Components
for _, component := range components.Headers {
if err = loader.resolveHeaderRef(doc, component, location); err != nil {
return
}
}
for _, component := range components.Parameters {
if err = loader.resolveParameterRef(doc, component, location); err != nil {
return
}
}
for _, component := range components.RequestBodies {
if err = loader.resolveRequestBodyRef(doc, component, location); err != nil {
return
}
}
for _, component := range components.Responses {
if err = loader.resolveResponseRef(doc, component, location); err != nil {
return
}
}
for _, component := range components.Schemas {
if err = loader.resolveSchemaRef(doc, component, location); err != nil {
return
}
}
for _, component := range components.SecuritySchemes {
if err = loader.resolveSecuritySchemeRef(doc, component, location); err != nil {
return
}
}
for _, component := range components.Examples {
if err = loader.resolveExampleRef(doc, component, location); err != nil {
return
}
}
for _, component := range components.Callbacks {
if err = loader.resolveCallbackRef(doc, component, location); err != nil {
return
}
}
// Visit all operations
for entrypoint, pathItem := range doc.Paths {
if pathItem == nil {
continue
}
if err = loader.resolvePathItemRef(doc, entrypoint, pathItem, location); err != nil {
return
}
}
return
}
func join(basePath *url.URL, relativePath *url.URL) (*url.URL, error) {
if basePath == nil {
return relativePath, nil
}
newPath, err := url.Parse(basePath.String())
if err != nil {
return nil, fmt.Errorf("cannot copy path: %q", basePath.String())
}
newPath.Path = path.Join(path.Dir(newPath.Path), relativePath.Path)
return newPath, nil
}
func resolvePath(basePath *url.URL, componentPath *url.URL) (*url.URL, error) {
if componentPath.Scheme == "" && componentPath.Host == "" {
// support absolute paths
if componentPath.Path[0] == '/' {
return componentPath, nil
}
return join(basePath, componentPath)
}
return componentPath, nil
}
func isSingleRefElement(ref string) bool {
return !strings.Contains(ref, "#")
}
func (loader *Loader) resolveComponent(
doc *T,
ref string,
path *url.URL,
resolved interface{},
) (
componentPath *url.URL,
err error,
) {
if doc, ref, componentPath, err = loader.resolveRef(doc, ref, path); err != nil {
return nil, err
}
parsedURL, err := url.Parse(ref)
if err != nil {
return nil, fmt.Errorf("cannot parse reference: %q: %v", ref, parsedURL)
}
fragment := parsedURL.Fragment
if !strings.HasPrefix(fragment, "/") {
return nil, fmt.Errorf("expected fragment prefix '#/' in URI %q", ref)
}
drill := func(cursor interface{}) (interface{}, error) {
for _, pathPart := range strings.Split(fragment[1:], "/") {
pathPart = unescapeRefString(pathPart)
if cursor, err = drillIntoField(cursor, pathPart); err != nil {
e := failedToResolveRefFragmentPart(ref, pathPart)
return nil, fmt.Errorf("%s: %s", e.Error(), err.Error())
}
if cursor == nil {
return nil, failedToResolveRefFragmentPart(ref, pathPart)
}
}
return cursor, nil
}
var cursor interface{}
if cursor, err = drill(doc); err != nil {
if path == nil {
return nil, err
}
var err2 error
data, err2 := loader.readURL(path)
if err2 != nil {
return nil, err
}
if err2 = yaml.Unmarshal(data, &cursor); err2 != nil {
return nil, err
}
if cursor, err2 = drill(cursor); err2 != nil || cursor == nil {
return nil, err
}
err = nil
}
switch {
case reflect.TypeOf(cursor) == reflect.TypeOf(resolved):
reflect.ValueOf(resolved).Elem().Set(reflect.ValueOf(cursor).Elem())
return componentPath, nil
case reflect.TypeOf(cursor) == reflect.TypeOf(map[string]interface{}{}):
codec := func(got, expect interface{}) error {
enc, err := json.Marshal(got)
if err != nil {
return err
}
if err = json.Unmarshal(enc, expect); err != nil {
return err
}
return nil
}
if err := codec(cursor, resolved); err != nil {
return nil, fmt.Errorf("bad data in %q", ref)
}
return componentPath, nil
default:
return nil, fmt.Errorf("bad data in %q", ref)
}
}
func drillIntoField(cursor interface{}, fieldName string) (interface{}, error) {
// Special case due to multijson
if s, ok := cursor.(*SchemaRef); ok && fieldName == "additionalProperties" {
if ap := s.Value.AdditionalPropertiesAllowed; ap != nil {
return *ap, nil
}
return s.Value.AdditionalProperties, nil
}
switch val := reflect.Indirect(reflect.ValueOf(cursor)); val.Kind() {
case reflect.Map:
elementValue := val.MapIndex(reflect.ValueOf(fieldName))
if !elementValue.IsValid() {
return nil, fmt.Errorf("map key %q not found", fieldName)
}
return elementValue.Interface(), nil
case reflect.Slice:
i, err := strconv.ParseUint(fieldName, 10, 32)
if err != nil {
return nil, err
}
index := int(i)
if 0 > index || index >= val.Len() {
return nil, errors.New("slice index out of bounds")
}
return val.Index(index).Interface(), nil
case reflect.Struct:
hasFields := false
for i := 0; i < val.NumField(); i++ {
hasFields = true
field := val.Type().Field(i)
tagValue := field.Tag.Get("yaml")
yamlKey := strings.Split(tagValue, ",")[0]
if yamlKey == "-" {
tagValue := field.Tag.Get("multijson")
yamlKey = strings.Split(tagValue, ",")[0]
}
if yamlKey == fieldName {
return val.Field(i).Interface(), nil
}
}
// if cursor is a "ref wrapper" struct (e.g. RequestBodyRef),
if _, ok := val.Type().FieldByName("Value"); ok {
// try digging into its Value field
return drillIntoField(val.FieldByName("Value").Interface(), fieldName)
}
if hasFields {
if ff := val.Type().Field(0); ff.PkgPath == "" && ff.Name == "ExtensionProps" {
extensions := val.Field(0).Interface().(ExtensionProps).Extensions
if enc, ok := extensions[fieldName]; ok {
var dec interface{}
if err := json.Unmarshal(enc.(json.RawMessage), &dec); err != nil {
return nil, err
}
return dec, nil
}
}
}
return nil, fmt.Errorf("struct field %q not found", fieldName)
default:
return nil, errors.New("not a map, slice nor struct")
}
}
func (loader *Loader) documentPathForRecursiveRef(current *url.URL, resolvedRef string) *url.URL {
if loader.rootDir == "" {
return current
}
return &url.URL{Path: path.Join(loader.rootDir, resolvedRef)}
}
func (loader *Loader) resolveRef(doc *T, ref string, path *url.URL) (*T, string, *url.URL, error) {
if ref != "" && ref[0] == '#' {
return doc, ref, path, nil
}
if err := loader.allowsExternalRefs(ref); err != nil {
return nil, "", nil, err
}
parsedURL, err := url.Parse(ref)
if err != nil {
return nil, "", nil, fmt.Errorf("cannot parse reference: %q: %v", ref, parsedURL)
}
fragment := parsedURL.Fragment
parsedURL.Fragment = ""
var resolvedPath *url.URL
if resolvedPath, err = resolvePath(path, parsedURL); err != nil {
return nil, "", nil, fmt.Errorf("error resolving path: %v", err)
}
if doc, err = loader.loadFromURIInternal(resolvedPath); err != nil {
return nil, "", nil, fmt.Errorf("error resolving reference %q: %v", ref, err)
}
return doc, "#" + fragment, resolvedPath, nil
}
func (loader *Loader) resolveHeaderRef(doc *T, component *HeaderRef, documentPath *url.URL) (err error) {
if component != nil && component.Value != nil {
if loader.visitedHeader == nil {
loader.visitedHeader = make(map[*Header]struct{})
}
if _, ok := loader.visitedHeader[component.Value]; ok {
return nil
}
loader.visitedHeader[component.Value] = struct{}{}
}
if component == nil {
return errors.New("invalid header: value MUST be an object")
}
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var header Header
if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, &header); err != nil {
return err
}
component.Value = &header
} else {
var resolved HeaderRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err := loader.resolveHeaderRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
documentPath = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
value := component.Value
if value == nil {
return nil
}
if schema := value.Schema; schema != nil {
if err := loader.resolveSchemaRef(doc, schema, documentPath); err != nil {
return err
}
}
return nil
}
func (loader *Loader) resolveParameterRef(doc *T, component *ParameterRef, documentPath *url.URL) (err error) {
if component != nil && component.Value != nil {
if loader.visitedParameter == nil {
loader.visitedParameter = make(map[*Parameter]struct{})
}
if _, ok := loader.visitedParameter[component.Value]; ok {
return nil
}
loader.visitedParameter[component.Value] = struct{}{}
}
if component == nil {
return errors.New("invalid parameter: value MUST be an object")
}
ref := component.Ref
if ref != "" {
if isSingleRefElement(ref) {
var param Parameter
if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, ¶m); err != nil {
return err
}
component.Value = ¶m
} else {
var resolved ParameterRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err := loader.resolveParameterRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
documentPath = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
value := component.Value
if value == nil {
return nil
}
if value.Content != nil && value.Schema != nil {
return errors.New("cannot contain both schema and content in a parameter")
}
for _, contentType := range value.Content {
if schema := contentType.Schema; schema != nil {
if err := loader.resolveSchemaRef(doc, schema, documentPath); err != nil {
return err
}
}
}
if schema := value.Schema; schema != nil {
if err := loader.resolveSchemaRef(doc, schema, documentPath); err != nil {
return err
}
}
return nil
}
func (loader *Loader) resolveRequestBodyRef(doc *T, component *RequestBodyRef, documentPath *url.URL) (err error) {
if component != nil && component.Value != nil {
if loader.visitedRequestBody == nil {
loader.visitedRequestBody = make(map[*RequestBody]struct{})
}
if _, ok := loader.visitedRequestBody[component.Value]; ok {
return nil
}
loader.visitedRequestBody[component.Value] = struct{}{}
}
if component == nil {
return errors.New("invalid requestBody: value MUST be an object")
}
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var requestBody RequestBody
if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, &requestBody); err != nil {
return err
}
component.Value = &requestBody
} else {
var resolved RequestBodyRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err = loader.resolveRequestBodyRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
documentPath = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
value := component.Value
if value == nil {
return nil
}
for _, contentType := range value.Content {
for name, example := range contentType.Examples {
if err := loader.resolveExampleRef(doc, example, documentPath); err != nil {
return err
}
contentType.Examples[name] = example
}
if schema := contentType.Schema; schema != nil {
if err := loader.resolveSchemaRef(doc, schema, documentPath); err != nil {
return err
}
}
}
return nil
}
func (loader *Loader) resolveResponseRef(doc *T, component *ResponseRef, documentPath *url.URL) (err error) {
if component != nil && component.Value != nil {
if loader.visitedResponse == nil {
loader.visitedResponse = make(map[*Response]struct{})
}
if _, ok := loader.visitedResponse[component.Value]; ok {
return nil
}
loader.visitedResponse[component.Value] = struct{}{}
}
if component == nil {
return errors.New("invalid response: value MUST be an object")
}
ref := component.Ref
if ref != "" {
if isSingleRefElement(ref) {
var resp Response
if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, &resp); err != nil {
return err
}
component.Value = &resp
} else {
var resolved ResponseRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err := loader.resolveResponseRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
documentPath = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
value := component.Value
if value == nil {
return nil
}
for _, header := range value.Headers {
if err := loader.resolveHeaderRef(doc, header, documentPath); err != nil {
return err
}
}
for _, contentType := range value.Content {
if contentType == nil {
continue
}
for name, example := range contentType.Examples {
if err := loader.resolveExampleRef(doc, example, documentPath); err != nil {
return err
}
contentType.Examples[name] = example
}
if schema := contentType.Schema; schema != nil {
if err := loader.resolveSchemaRef(doc, schema, documentPath); err != nil {
return err
}
contentType.Schema = schema
}
}
for _, link := range value.Links {
if err := loader.resolveLinkRef(doc, link, documentPath); err != nil {
return err
}
}
return nil
}
func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPath *url.URL) (err error) {
if component != nil && component.Value != nil {
if loader.visitedSchema == nil {
loader.visitedSchema = make(map[*Schema]struct{})
}
if _, ok := loader.visitedSchema[component.Value]; ok {
return nil
}
loader.visitedSchema[component.Value] = struct{}{}
}
if component == nil {
return errors.New("invalid schema: value MUST be an object")
}
ref := component.Ref
if ref != "" {
if isSingleRefElement(ref) {
var schema Schema
if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, &schema); err != nil {
return err
}
component.Value = &schema
} else {
var resolved SchemaRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err := loader.resolveSchemaRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
documentPath = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
value := component.Value
if value == nil {
return nil
}
// ResolveRefs referred schemas
if v := value.Items; v != nil {
if err := loader.resolveSchemaRef(doc, v, documentPath); err != nil {
return err
}
}
for _, v := range value.Properties {
if err := loader.resolveSchemaRef(doc, v, documentPath); err != nil {
return err
}
}
if v := value.AdditionalProperties; v != nil {
if err := loader.resolveSchemaRef(doc, v, documentPath); err != nil {
return err
}
}
if v := value.Not; v != nil {
if err := loader.resolveSchemaRef(doc, v, documentPath); err != nil {
return err
}
}
for _, v := range value.AllOf {
if err := loader.resolveSchemaRef(doc, v, documentPath); err != nil {
return err
}
}
for _, v := range value.AnyOf {
if err := loader.resolveSchemaRef(doc, v, documentPath); err != nil {
return err
}
}
for _, v := range value.OneOf {
if err := loader.resolveSchemaRef(doc, v, documentPath); err != nil {
return err
}
}
return nil
}
func (loader *Loader) resolveSecuritySchemeRef(doc *T, component *SecuritySchemeRef, documentPath *url.URL) (err error) {
if component != nil && component.Value != nil {
if loader.visitedSecurityScheme == nil {
loader.visitedSecurityScheme = make(map[*SecurityScheme]struct{})
}
if _, ok := loader.visitedSecurityScheme[component.Value]; ok {
return nil
}
loader.visitedSecurityScheme[component.Value] = struct{}{}
}
if component == nil {
return errors.New("invalid securityScheme: value MUST be an object")
}
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var scheme SecurityScheme
if _, err = loader.loadSingleElementFromURI(ref, documentPath, &scheme); err != nil {
return err
}
component.Value = &scheme
} else {
var resolved SecuritySchemeRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err := loader.resolveSecuritySchemeRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
_ = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
return nil
}
func (loader *Loader) resolveExampleRef(doc *T, component *ExampleRef, documentPath *url.URL) (err error) {
if component != nil && component.Value != nil {
if loader.visitedExample == nil {
loader.visitedExample = make(map[*Example]struct{})
}
if _, ok := loader.visitedExample[component.Value]; ok {
return nil
}
loader.visitedExample[component.Value] = struct{}{}
}
if component == nil {
return errors.New("invalid example: value MUST be an object")
}
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var example Example
if _, err = loader.loadSingleElementFromURI(ref, documentPath, &example); err != nil {
return err
}
component.Value = &example
} else {
var resolved ExampleRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err := loader.resolveExampleRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
_ = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
return nil
}
func (loader *Loader) resolveCallbackRef(doc *T, component *CallbackRef, documentPath *url.URL) (err error) {
if component == nil {
return errors.New("invalid callback: value MUST be an object")
}
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var resolved Callback
if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, &resolved); err != nil {
return err
}
component.Value = &resolved
} else {
var resolved CallbackRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err := loader.resolveCallbackRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
documentPath = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
value := component.Value
if value == nil {
return nil
}
for entrypoint, pathItem := range *value {
entrypoint, pathItem := entrypoint, pathItem
err = func() (err error) {
key := "-"
if documentPath != nil {
key = documentPath.EscapedPath()
}
key += entrypoint
if _, ok := loader.visitedPathItemRefs[key]; ok {
return nil
}
loader.visitedPathItemRefs[key] = struct{}{}
if pathItem == nil {
return errors.New("invalid path item: value MUST be an object")
}
ref := pathItem.Ref
if ref != "" {
if isSingleRefElement(ref) {
var p PathItem
if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, &p); err != nil {
return err
}
*pathItem = p
} else {
if doc, ref, documentPath, err = loader.resolveRef(doc, ref, documentPath); err != nil {
return
}
rest := strings.TrimPrefix(ref, "#/components/callbacks/")
if rest == ref {
return fmt.Errorf(`expected prefix "#/components/callbacks/" in URI %q`, ref)
}
id := unescapeRefString(rest)
definitions := doc.Components.Callbacks
if definitions == nil {
return failedToResolveRefFragmentPart(ref, "callbacks")
}
resolved := definitions[id]
if resolved == nil {
return failedToResolveRefFragmentPart(ref, id)
}
for _, p := range *resolved.Value {
*pathItem = *p
break
}
}
}
return loader.resolvePathItemRefContinued(doc, pathItem, documentPath)
}()
if err != nil {
return err
}
}
return nil
}
func (loader *Loader) resolveLinkRef(doc *T, component *LinkRef, documentPath *url.URL) (err error) {
if component != nil && component.Value != nil {
if loader.visitedLink == nil {
loader.visitedLink = make(map[*Link]struct{})
}
if _, ok := loader.visitedLink[component.Value]; ok {
return nil
}
loader.visitedLink[component.Value] = struct{}{}
}
if component == nil {
return errors.New("invalid link: value MUST be an object")
}
if ref := component.Ref; ref != "" {
if isSingleRefElement(ref) {
var link Link
if _, err = loader.loadSingleElementFromURI(ref, documentPath, &link); err != nil {
return err
}
component.Value = &link
} else {
var resolved LinkRef
componentPath, err := loader.resolveComponent(doc, ref, documentPath, &resolved)
if err != nil {
return err
}
if err := loader.resolveLinkRef(doc, &resolved, componentPath); err != nil {
return err
}
component.Value = resolved.Value
_ = loader.documentPathForRecursiveRef(documentPath, resolved.Ref)
}
}
return nil
}
func (loader *Loader) resolvePathItemRef(doc *T, entrypoint string, pathItem *PathItem, documentPath *url.URL) (err error) {
key := "_"
if documentPath != nil {
key = documentPath.EscapedPath()
}
key += entrypoint
if _, ok := loader.visitedPathItemRefs[key]; ok {
return nil
}
loader.visitedPathItemRefs[key] = struct{}{}
if pathItem == nil {
return errors.New("invalid path item: value MUST be an object")
}
ref := pathItem.Ref
if ref != "" {
if isSingleRefElement(ref) {
var p PathItem
if documentPath, err = loader.loadSingleElementFromURI(ref, documentPath, &p); err != nil {
return err
}
*pathItem = p
} else {
if doc, ref, documentPath, err = loader.resolveRef(doc, ref, documentPath); err != nil {
return
}
rest := strings.TrimPrefix(ref, "#/paths/")
if rest == ref {
return fmt.Errorf(`expected prefix "#/paths/" in URI %q`, ref)
}
id := unescapeRefString(rest)
definitions := doc.Paths
if definitions == nil {