Repository navigation
Expand file tree
/
Copy pathrefs.go
More file actions
1070 lines (968 loc) · 28.5 KB
/
Copy pathrefs.go
File metadata and controls
1070 lines (968 loc) · 28.5 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
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 main
import (
"cmp"
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"slices"
"strconv"
"strings"
"github.com/mohae/deepcopy"
"github.com/dolmen-go/jsonptr"
)
func skipRef(ptr jsonptr.Pointer) bool {
if len(ptr) < 1 {
return false
}
last := ptr[len(ptr)-1]
return last == "properties" ||
last == "additionalProperties"
}
// visitRefs visits $ref and allows to change them.
func visitRefs(root any, ptr jsonptr.Pointer, visitor func(jsonptr.Pointer, string) (string, error)) (err error) {
//log.Println(ptr)
switch root := root.(type) {
case map[string]any:
if len(root) == 0 {
break
}
ptr.Grow(1)
for _, k := range sortedKeys(root) {
ptr.Property(k)
if k == "$ref" && !skipRef(ptr[:len(ptr)-1]) {
if str, isString := root[k].(string); isString {
root[k], err = visitor(ptr, str)
if err != nil {
return
}
}
} else {
err = visitRefs(root[k], ptr, visitor)
if err != nil {
break
}
}
ptr.Up()
}
case []any:
if len(root) == 0 {
break
}
ptr.Grow(1)
for i, v := range root {
ptr.Index(i)
// log.Println(ptr)
err = visitRefs(v, ptr, visitor)
if err != nil {
break
}
ptr.Up()
}
}
return
}
// loc represents the location of a JSON node.
type loc struct {
Path string
Ptr string
}
func (l *loc) URL() *url.URL {
u := url.URL{
Path: l.Path,
Fragment: l.Ptr,
}
if l.Path[0] == '/' {
u.Scheme = "file"
}
return &u
}
func (l loc) String() string {
//return l.URL().String()
if l.Ptr == "" {
return l.Path
}
return l.Path + "#" + l.Ptr
}
/*
func (l *loc) Pointer() jsonptr.Pointer {
ptr, _ := jsonptr.Parse(l.Ptr)
return ptr
}
*/
func (l *loc) Property(name string) loc {
return loc{
Path: l.Path,
//Ptr: l.Ptr + "/" + jsonptr.EscapeString(name),
Ptr: string(jsonptr.AppendEscape(append([]byte(l.Ptr), '/'), name)),
}
}
func (l *loc) Index(i int) loc {
return loc{
Path: l.Path,
//Ptr: l.Ptr + "/" + strconv.Itoa(i),
Ptr: string(strconv.AppendInt(append([]byte(l.Ptr), '/'), int64(i), 10)),
}
}
func (l *loc) Rel(basePath string) loc {
// FIXME do not use FS dependent paths
rel, err := filepath.Rel(urlPathToOSPath(basePath), urlPathToOSPath(l.Path))
if err != nil {
return *l
}
return loc{rel, l.Ptr}
}
type setter func(any)
type node struct {
data any
set setter
loc loc
}
// IsRef returns true if the node is a $ref.
func (n *node) IsRef() bool {
obj, isObj := n.data.(map[string]any)
if !isObj || obj == nil {
return false
}
link, isString := obj["$ref"].(string)
if !isString {
return false
}
return len(link) > 0
}
// Ref returns the link of a $ref node.
func (n *node) Ref() string {
obj, isObj := n.data.(map[string]any)
if !isObj || obj == nil {
return ""
}
link, isString := obj["$ref"].(string)
if !isString {
return ""
}
return link
}
type refResolver struct {
basePath string // absolute path to make errors relative to
rootPath string
docs map[string]*any // path -> rdoc
visited map[loc]bool
// expanding are the nodes being expanded (in the call stack)
expanding map[loc]bool
inject map[string]injection // pointer in the final document -> source
deferred []deferredLink
inlining bool
trace func(string)
swagger bool // Swagger 2.0 (else OpenAPI 3.x)
// securitySchemes is the location of security schemes:
// "/components/securitySchemes/" (OpenAPI 3.x) or "/securityDefinitions/" (Swagger 2.0)
securitySchemes string
}
// swaggerComponents are the containers of reusable components in Swagger 2.0.
var swaggerComponents = map[string]bool{
"definitions": true,
"parameters": true,
"responses": true,
"securityDefinitions": true,
}
// injection is content to copy, at the same pointer, from a document to the final document.
type injection struct {
src string // path of the source document
from loc // location of the link which requires the injection
}
// deferredLink is a relative link which target doesn't exist in the document where
// it is used: its resolution is deferred to the final document.
type deferredLink struct {
link string // "#<pointer>"
from loc // location of the link
}
// operationMethods are the keys of operations in a Path Item Object.
var operationMethods = map[string]bool{
"get": true,
"put": true,
"post": true,
"delete": true,
"options": true,
"head": true,
"patch": true,
"trace": true,
"query": true, // OpenAPI 3.2
}
// isSecurityPtr reports whether ptr is the location of a list of Security Requirement Objects:
// at the root of a document or in an Operation Object (see [isOperationPtr]).
func isSecurityPtr(ptr jsonptr.Pointer) bool {
n := len(ptr)
if n == 0 || ptr[n-1] != "security" {
return false
}
return n == 1 || isOperationPtr(ptr[:n-1])
}
// isOperationPtr reports whether ptr is the location of an Operation Object, from
// the structure of an OpenAPI document. The whole pointer is checked, so that data
// that looks like an OpenAPI document (ex: in an example) doesn't match:
//
// /paths/{path}/{method}
// /webhooks/{name}/{method} (OpenAPI 3.1)
// /components/pathItems/{name}/{method} (OpenAPI 3.1)
// /components/callbacks/{name}/{expression}/{method}
// {operation}/callbacks/{name}/{expression}/{method}
func isOperationPtr(ptr jsonptr.Pointer) bool {
n := len(ptr)
if n < 3 || !operationMethods[ptr[n-1]] {
return false
}
switch {
case n == 3:
return ptr[0] == "paths" || ptr[0] == "webhooks"
case n == 4:
return ptr[0] == "components" && ptr[1] == "pathItems"
case ptr[n-4] == "callbacks":
parent := ptr[:n-4]
return (len(parent) == 1 && parent[0] == "components") || isOperationPtr(parent)
}
return false
}
// isNotFound reports whether err (from resolve) is because the target of the link doesn't exist.
func isNotFound(err error) bool {
var errExp *errExpand
if errors.As(err, &errExp) {
// Failure while expanding something else on the way
return false
}
return errors.Is(err, jsonptr.ErrProperty) || errors.Is(err, jsonptr.ErrIndex)
}
type errExpand struct {
loc loc
err error
}
func (e *errExpand) Error() string {
return e.loc.String() + ": " + e.err.Error()
}
func (e *errExpand) Unwrap() error {
return e.err
}
func (resolver *refResolver) Error(loc *loc, err error) error {
return &errExpand{loc.Rel(resolver.basePath), err}
}
func (resolver *refResolver) Errorf(loc *loc, msg string, args ...any) error {
var err error
if len(args) == 0 {
err = errors.New(msg)
} else {
err = fmt.Errorf(msg, args...)
}
return resolver.Error(loc, err)
}
func (resolver *refResolver) Tracef(msg string, args ...any) {
if resolver.trace == nil {
return
}
resolver.trace(fmt.Sprintf(msg, args...))
}
func (resolver *refResolver) resolve(link string, relativeTo *loc) (*node, error) {
// log.Println(link, relativeTo)
var targetLoc loc
var ptr jsonptr.Pointer
var err error
if before, after, ok := strings.Cut(link, "#"); ok {
targetLoc.Path = before
targetLoc.Ptr = after
ptr, err = jsonptr.Parse(targetLoc.Ptr)
if err != nil {
return nil, fmt.Errorf("%q: %v", targetLoc.Ptr, err)
}
} else {
targetLoc.Path = link
}
if len(targetLoc.Path) > 0 {
tmpPath, err := url.PathUnescape(targetLoc.Path)
if err != nil {
return nil, fmt.Errorf("%q: %v", targetLoc.Path, err)
}
targetLoc.Path = resolvePath(relativeTo.Path, tmpPath)
} else {
targetLoc.Path = relativeTo.Path
}
// log.Println("=>", u)
rdoc, loaded := resolver.docs[targetLoc.Path]
if !loaded {
//log.Println("Loading", &targetLoc)
doc, err := loadFile(urlPathToOSPath(targetLoc.Path))
if err != nil {
return nil, fmt.Errorf("can't load %q: %v", targetLoc.Path, err)
}
var itf any = doc
rdoc = &itf
resolver.docs[targetLoc.Path] = rdoc
}
if targetLoc.Ptr == "" {
return &node{*rdoc, func(data any) {
*rdoc = data
}, targetLoc}, nil
}
// FIXME we could reduce the number of evals of JSON pointers...
frag, err := ptr.In(*rdoc)
if err != nil {
// If the can't be immediately resolved, this may be because
// of a $inline in the way
p := jsonptr.Pointer{}
for {
// log.Println(p)
doc, err := p.In(*rdoc)
if err != nil {
// Failed to resolve the fragment
return nil, err
}
if obj, isMap := doc.(map[string]any); isMap {
if _, isInline := obj["$inline"]; isInline {
//log.Printf("%#v", obj)
err := resolver.expand(node{obj, func(data any) {
p.Set(rdoc, data)
}, loc{Path: targetLoc.Path, Ptr: p.String()}})
if err != nil {
return nil, err
}
}
}
if len(p) == len(ptr) {
break
}
p = ptr[:len(p)+1]
}
frag, _ = ptr.In(*rdoc)
}
return &node{frag, func(data any) {
ptr.Set(rdoc, data)
}, targetLoc}, nil
}
func (resolver *refResolver) expand(n node) error {
resolver.Tracef("%14s %s", n.loc.Path[strings.LastIndexByte(n.loc.Path, '/')+1:], n.loc.Ptr)
if resolver.visited[n.loc] {
return nil
}
if !resolver.inlining {
resolver.visited[n.loc] = true
}
// A node already being expanded (up in the stack) is reached again through a
// recursive link (ex: recursive schema). This matters while inlining, as
// visited isn't updated.
if resolver.expanding[n.loc] {
return nil
}
resolver.expanding[n.loc] = true
defer delete(resolver.expanding, n.loc)
if doc, isSlice := n.data.([]any); isSlice {
if isSecurityPtr(jsonptr.MustParse(n.loc.Ptr)) {
if err := resolver.expandSecurity(n.loc, doc); err != nil {
return err
}
}
for i, v := range doc {
switch v.(type) {
case []any, map[string]any:
err := resolver.expand(node{v, func(data any) {
doc[i] = data
}, n.loc.Index(i)})
if err != nil {
return err
}
}
}
return nil
}
obj, isObject := n.data.(map[string]any)
if !isObject || obj == nil {
return nil
}
if ref, isRef := obj["$ref"]; isRef && !skipRef(jsonptr.MustParse(n.loc.Ptr)) {
return resolver.expandTagRef(obj, n.set, &n.loc, ref)
}
// An extension to build an object from mixed local data and
// imported data
if refs, isMerge := obj["$merge"]; isMerge {
return resolver.expandTagMerge(obj, n.set, &n.loc, refs)
}
if ref, isInline := obj["$inline"]; isInline {
return resolver.expandTagInline(obj, n.set, &n.loc, ref)
}
for _, k := range sortedKeys(obj) {
// /components are expanded only on demand (when targeted by a link)
// because expanding unused components would inject their external
// content into the final document.
if k == "components" && n.loc.Ptr == "" {
continue
}
if err := resolver.expandProperty(n.loc, obj, k); err != nil {
return err
}
}
return nil
}
func (resolver *refResolver) expandProperty(parentLoc loc, obj map[string]any, key string) error {
//log.Println("Key:", key)
return resolver.expand(node{obj[key], func(data any) {
obj[key] = data
}, parentLoc.Property(key)})
}
// expandTagRef expands (follows) a $ref link.
func (resolver *refResolver) expandTagRef(obj map[string]any, set setter, l *loc, ref any) error {
resolver.Tracef("$ref: %s => %s", l, ref)
link, isString := ref.(string)
if !isString {
return resolver.Errorf(&loc{l.Path, l.Ptr + "/$ref"}, "must be a string")
}
if len(obj) > 1 {
unexpected := len(obj) - 1 // Don't count $ref
// http://spec.openapis.org/oas/v3.1.0#reference-object
if _, ok := stringProp(obj, "summary"); ok {
unexpected--
}
if _, ok := stringProp(obj, "description"); ok {
unexpected--
}
// Non-standard, but that's not our job to validate spec
if _, ok := stringProp(obj, "$comment"); ok {
unexpected--
}
if unexpected > 0 {
return resolver.Errorf(l, "$ref must be alone (tip: use $merge instead)")
}
}
target, err := resolver.importLink(link, l)
if err != nil || target == nil { // target == nil: deferred
return err
}
// A link to an ancestor is fine (recursive schema), except to a whole document
if target.loc.Ptr == "" {
return resolver.Errorf(l, "injection of %q at root will create a circular link (tip: use $inline)", target.loc.Path)
}
if target.loc == *l {
return resolver.Errorf(l, "circular link: $ref to itself")
}
return nil
}
// importLink resolves link from l, expands its target and records its injection into
// the final document.
//
// If link is relative and its target doesn't exist, the link is deferred and the returned node is nil.
func (resolver *refResolver) importLink(link string, l *loc) (*node, error) {
target, err := resolver.resolve(link, l)
if err != nil {
if isRelativeLink(link) && isNotFound(err) {
resolver.deferLink(link, l)
return nil, nil
}
if _, isExpandErr := err.(*errExpand); !isExpandErr {
err = resolver.Error(l, err)
}
return nil, err
}
imported, err := resolver.expandTarget(target)
if err != nil {
return nil, err
}
if err = resolver.recordInjection(imported, l, link); err != nil {
return nil, err
}
return target, nil
}
// enclosingComponent returns the pointer of the component which contains ptr
// (/components/<type>/<name>, or /<container>/<name> in Swagger 2.0) if ptr is
// a part of a component (and not the component itself).
func (resolver *refResolver) enclosingComponent(ptr string) (string, bool) {
p := jsonptr.MustParse(ptr)
switch {
case resolver.swagger:
if len(p) > 2 && swaggerComponents[p[0]] {
return p[:2].String(), true
}
case len(p) > 3 && p[0] == "components":
return p[:3].String(), true
}
return "", false
}
// expandTarget expands the target of a link and returns the node to import.
//
// If the target is a part of a component, the whole component is expanded and
// imported, as the target is meaningful only in its component.
func (resolver *refResolver) expandTarget(target *node) (*node, error) {
comp, isPart := resolver.enclosingComponent(target.loc.Ptr)
if !isPart {
return target, resolver.expandNode(target)
}
compNode, err := resolver.resolve("#"+comp, &loc{Path: target.loc.Path})
if err != nil {
if _, isExpandErr := err.(*errExpand); !isExpandErr {
err = resolver.Error(&target.loc, err)
}
return nil, err
}
if err = resolver.expandNode(compNode); err != nil {
return nil, err
}
return compNode, nil
}
// isRelativeLink reports whether link is relative to the document where it is used ("#<pointer>").
//
// The target of a relative link is imported from that document if it exists there. Else its
// resolution is deferred to the final document (see [refResolver.deferLink]).
func isRelativeLink(link string) bool {
return len(link) > 0 && link[0] == '#'
}
// deferLink records a relative link which target doesn't exist in the document
// where it is used. It will be resolved in the final document.
func (resolver *refResolver) deferLink(link string, l *loc) {
resolver.Tracef("deferred: %s => %s", l, link)
resolver.deferred = append(resolver.deferred, deferredLink{link: link, from: *l})
}
// recordInjection records that target, linked from l, must be copied to the final document
// at the same pointer, if it isn't already in the root document.
func (resolver *refResolver) recordInjection(target *node, l *loc, link string) error {
if target.loc.Path == resolver.rootPath {
return nil
}
ptr, src := target.loc.Ptr, target.loc.Path
if prev, exists := resolver.inject[ptr]; exists && prev.src != src {
// Chain of $ref at the same pointer: keep the final target
if resolver.linksTo(target.data, src, prev.src, ptr) {
return nil
}
prevContent, err := jsonptr.Get(*resolver.docs[prev.src], ptr)
if err != nil || !resolver.linksTo(prevContent, prev.src, src, ptr) {
return resolver.Errorf(l, "import fragment %q is imported from %s and %s (from %s)",
link, resolver.relPath(prev.src), resolver.relPath(src), prev.from.Rel(resolver.basePath))
}
} else if exists {
return nil
}
resolver.inject[ptr] = injection{src: src, from: *l}
return nil
}
// expandSecurity expands the security schemes named in a list of Security Requirement Objects.
//
// A name is a link relative to the document where the requirement is used.
func (resolver *refResolver) expandSecurity(l loc, reqs []any) error {
for i, req := range reqs {
req, isObj := req.(map[string]any)
if !isObj {
continue
}
lReq := l.Index(i)
for _, name := range sortedKeys(req) {
link := "#" + resolver.securitySchemes + jsonptr.EscapeString(name)
lName := lReq.Property(name)
if _, err := resolver.importLink(link, &lName); err != nil {
return err
}
}
}
return nil
}
// expandTagMerge expands a $merge object.
func (resolver *refResolver) expandTagMerge(obj map[string]any, set setter, l *loc, refs any) error {
resolver.Tracef("$merge at %s", l)
if _, isInline := obj["$inline"]; isInline {
return resolver.Errorf(l, "$merge and $inline can't be used together")
}
var links []string
switch refs := refs.(type) {
case string:
if len(obj) == 1 {
return resolver.Errorf(l, "merging with nothing?")
}
links = []string{refs}
case []any:
links = make([]string, len(refs))
for i, v := range refs {
lnk, isString := v.(string)
if !isString {
return resolver.Errorf(&loc{l.Path, fmt.Sprintf("%s/%d", l.Ptr, i)}, "must be a string")
}
// Reverse order
links[len(links)-1-i] = lnk
}
if len(links) == 1 && len(obj) == 1 {
return resolver.Errorf(l, "merging with nothing? (tip: use $inline)")
}
default:
return resolver.Errorf(&loc{l.Path, l.Ptr + "/$merge"}, "must be a string or array of strings")
}
delete(obj, "$merge")
// Expand again the same node, now without $merge
delete(resolver.visited, *l)
delete(resolver.expanding, *l)
err := resolver.expand(node{obj, func(data any) {
obj = data.(map[string]any)
set(data)
}, *l})
resolver.visited[*l] = true
resolver.expanding[*l] = true // Restore for the caller's deferred cleanup
if err != nil {
return err
}
// overrides := make(map[string]string)
// fill with (key => loc.Property(key))
for i, link := range links {
target, err := resolver.resolveAndExpand(link, l)
if err != nil {
return err
}
objTarget, isObj := target.data.(map[string]any)
if !isObj {
if len(links) == 1 {
return resolver.Errorf(&loc{l.Path, l.Ptr + "/$merge"}, "link must point to object")
}
return resolver.Errorf(&loc{l.Path, fmt.Sprintf("%s/$merge/%d", l.Ptr, i)}, "link must point to object")
}
for k, v := range objTarget {
if _, exists := obj[k]; exists {
// TODO warn about overrides if verbose
// if o, overriden := overrides[k]; overriden {
// log.Println("%s overrides %s", l.Property(k), target.loc.Property(k))
// }
continue
}
obj[k] = v
// overrides[k] = link
}
}
return nil
}
var replDollar = strings.NewReplacer("~2", "$")
type patch struct {
key string
ptr jsonptr.Pointer
value any
}
func sortedPatches(obj map[string]any) ([]*patch, error) {
patches := make([]*patch, 0, len(obj))
for k, v := range obj {
if len(k) > 0 && k[0] == '$' {
continue
}
// To forbid raw '$' (because we have '$inline'), but still enable it
// in pointers, we use "~2" as a replacement as it is not a valid JSON Pointer
// sequence.
ptr, err := jsonptr.Parse("/" + replDollar.Replace(k))
if err != nil {
return nil, fmt.Errorf("patch %q: %w", k, err)
}
patches = append(patches, &patch{
key: k,
ptr: ptr,
value: v,
})
}
slices.SortStableFunc(patches, func(a, b *patch) int {
if x := cmp.Compare(len(a.ptr), len(b.ptr)); x != 0 {
return x
}
for i := range len(a.ptr) {
if x := cmp.Compare(a.ptr[i], b.ptr[i]); x != 0 {
return x
}
}
return cmp.Compare(a.key, b.key) // "~2" vs "$"
})
return patches, nil
}
// expandTagInline expands a $inline object.
func (resolver *refResolver) expandTagInline(obj map[string]any, set setter, l *loc, ref any) error {
resolver.Tracef("$inline: %s => %s", l, ref)
link, isString := ref.(string)
if !isString {
return resolver.Errorf(&loc{l.Path, l.Ptr + "/$inline"}, "must be a string")
}
inlining := resolver.inlining
resolver.inlining = true
var target *node
var err error
l2 := loc{l.Path, l.Ptr} // Clone
seen := map[loc]bool{} // Targets of the chain of $ref
for {
target, err = resolver.resolveAndExpand(link, &l2)
if err != nil {
return err
}
// If target is not $ref, stop
link = target.Ref()
if link == "" || len(obj) == 1 {
break
}
if seen[target.loc] {
return resolver.Errorf(&loc{l.Path, l.Ptr + "/$inline"}, "circular chain of $ref at %s", target.loc.Rel(resolver.basePath))
}
seen[target.loc] = true
// Else loop to dereference it
l2 = loc{target.loc.Path, target.loc.Ptr}
}
resolver.inlining = inlining
target.data = deepcopy.Copy(target.data)
// Replace the original node (obj) with the copy of the target
set(target.data)
// obj is now disconnected from the original tree
//log.Printf("xxx %#v", target.data)
if len(obj) > 1 {
switch target.data.(type) {
case map[string]any, []any:
patches, err := sortedPatches(obj)
if err != nil {
return resolver.Error(l, err)
}
for _, p := range patches {
v := p.value
err = resolver.expand(node{v, func(data any) {
v = data
}, l.Property(p.key)})
if err != nil {
return err
}
// Preserve the source, as it may be patched later
v = deepcopy.Copy(v)
if err := p.ptr.Set(&target.data, v); err != nil {
l2 := l.Property(p.key)
return resolver.Error(&l2, err)
}
// If slice, it may have been appended
set(target.data)
}
default:
return resolver.Errorf(l, "inlined scalar value can't be patched")
}
}
return nil
}
// resolveAndExpand resolves and expands the target of a link which content is copied
// ($inline, $merge).
//
// The target can't be an ancestor of the link: copying it would create an infinite tree.
// (A $ref to an ancestor is fine: this is a recursive schema).
func (resolver *refResolver) resolveAndExpand(link string, relativeTo *loc) (n *node, err error) {
n, err = resolver.resolve(link, relativeTo)
if err == nil && n.loc.Path == relativeTo.Path && strings.HasPrefix(relativeTo.Ptr, n.loc.Ptr+"/") {
err = errors.New("circular link")
}
if err != nil {
if _, isExpandErr := err.(*errExpand); !isExpandErr {
err = resolver.Error(relativeTo, err)
}
} else {
err = resolver.expandNode(n)
}
return
}
// expandNode expands n and updates n.data if the node is replaced (ex: $inline).
func (resolver *refResolver) expandNode(n *node) error {
set := n.set
n.set = func(data any) {
n.data = data
set(data)
}
return resolver.expand(*n)
}
func ExpandRefs(rdoc *any, docURL *url.URL, trace func(string)) error {
if len(docURL.Fragment) > 0 {
panic("URL fragment unexpected for initial document")
}
cwd, _ := os.Getwd()
path := path.Clean(docURL.Path)
resolver := refResolver{
basePath: osPathToURLPath(cwd),
rootPath: path,
docs: map[string]*any{
path: rdoc,
},
inject: make(map[string]injection),
visited: make(map[loc]bool),
expanding: make(map[loc]bool),
trace: trace,
securitySchemes: "/components/securitySchemes/",
}
if root, isObj := (*rdoc).(map[string]any); isObj {
if _, resolver.swagger = root["swagger"]; resolver.swagger {
resolver.securitySchemes = "/securityDefinitions/"
}
}
// First step:
// - load referenced documents
// - collect links to content to import from other documents
// - collect relative links which target doesn't exist where they are used
// - replace $inline, $merge
err := resolver.expand(node{*rdoc, func(data any) {
*rdoc = data
}, loc{Path: path}})
if err != nil {
return err
}
// Second step:
// Expand the targets of deferred links that exist in the root document
// (as /components are expanded only on demand). This may defer more links.
rootLoc := loc{Path: path}
for i := 0; i < len(resolver.deferred); i++ {
d := resolver.deferred[i]
if d.from.Path == path {
continue // Already not found in the root document
}
target, err := resolver.resolve(d.link, &rootLoc)
if err != nil {
if isNotFound(err) {
continue
}
if _, isExpandErr := err.(*errExpand); !isExpandErr {
err = resolver.Error(&d.from, err)
}
return err
}
if _, err = resolver.expandTarget(target); err != nil {
return err
}
}
// Third step:
// Inject content imported from other documents, at the same pointer.
if err = resolver.injectAll(rdoc); err != nil {
return err
}
// Fourth step:
// Check that the targets of deferred links exist in the final document.
for _, d := range resolver.deferred {
if _, err := jsonptr.Get(*rdoc, d.link[1:]); err != nil {
return resolver.Errorf(&d.from, "%q: not found in the final document", d.link)
}
}
// Fifth step:
// As some $ref pointed to external documents we have to fix them to make the references
// local.
if len(resolver.docs) > 1 {
_ = visitRefs(*rdoc, nil, func(ptr jsonptr.Pointer, ref string) (string, error) {
i := strings.IndexByte(ref, '#')
if i > 0 {
ref = ref[i:]
}
return ref, nil
})
}
return nil
}
// injectAll copies content imported from other documents into the final document *rdoc,
// at the same pointer.
//
// The location must either not exist (missing parents are created) or be a $ref to
// the imported content. Else this is a conflict.
func (resolver *refResolver) injectAll(rdoc *any) error {
type imported struct {
ptr jsonptr.Pointer
injection
}
imports := make([]imported, 0, len(resolver.inject))
for p, inj := range resolver.inject {
imports = append(imports, imported{jsonptr.MustParse(p), inj})
}
// Shallow pointers first, so we know where the content of deeper pointers comes from
slices.SortFunc(imports, func(a, b imported) int {
if x := cmp.Compare(len(a.ptr), len(b.ptr)); x != 0 {
return x
}
return slices.Compare(a.ptr, b.ptr)
})
injected := make(map[string]string, len(imports)) // pointer -> source document
for _, imp := range imports {
ptr := imp.ptr.String()
// The document where the current content at ptr comes from
base := resolver.rootPath
for i := len(imp.ptr) - 1; i >= 0; i-- {
if src, isInjected := injected[imp.ptr[:i].String()]; isInjected {
base = src
break
}
}
if base == imp.src {
continue // Already imported with a parent
}
content, err := imp.ptr.In(*resolver.docs[imp.src])
if err != nil {
return resolver.Errorf(&imp.from, "%s#%s has disappeared after replacement of $inline and $merge: %v", resolver.relPath(imp.src), ptr, err)
}
if current, err := imp.ptr.In(*rdoc); err == nil && !resolver.linksTo(current, base, imp.src, ptr) {
return resolver.Errorf(&imp.from, "%s: conflict between content imported from %s and content from %s",
ptr, resolver.relPath(imp.src), resolver.relPath(base))
}
if err = resolver.importAt(rdoc, imp.ptr, content); err != nil {
return resolver.Errorf(&imp.from, "%s#%s: %v", resolver.relPath(imp.src), ptr, err)
}
injected[ptr] = imp.src
}
return nil
}
// relPath returns the path of a document relative to the base path, for error messages.
func (resolver *refResolver) relPath(pth string) string {
l := loc{Path: pth}
return l.Rel(resolver.basePath).Path