-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreferences_resolve.go
119 lines (91 loc) · 2.67 KB
/
references_resolve.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
package openapi
import (
"fmt"
"strings"
"github.com/MarkRosemaker/errpath"
)
type ref []string
func (r ref) String() string {
return strings.Join(r, "/")
}
// collectResolveRefs expands references in a document that was just unmarshaled
func (l *loader) collectResolveRefs(doc *Document) error {
// collect all the references
l.collectDocument(doc, []string{"#"})
// resolve all the references
if err := l.resolveDocument(doc); err != nil {
return err
}
return nil
}
// resolveRef resolves a reference to a value or resolves the value itself
func resolveRef[T any, O referencable[T]](
r *refOrValue[T, O], values map[string]*T, resolveValue func(*T) error,
) error {
if r.Ref != nil && r.Value == nil {
if val, ok := values[r.Ref.Identifier]; ok {
r.Value = val
return nil
}
return fmt.Errorf("couldn't resolve %q", r.Ref.Identifier)
}
if resolveValue == nil {
return nil
}
return resolveValue(r.Value)
}
func (l *loader) collectPaths(ps Paths, ref ref) {
}
func (l *loader) resolveOperation(o *Operation) error {
if err := l.resolveParameterList(o.Parameters); err != nil {
return &errpath.ErrField{Field: "parameters", Err: err}
}
if o.RequestBody != nil {
if err := l.resolveRequestBodyRef(o.RequestBody); err != nil {
return &errpath.ErrField{Field: "requestBody", Err: err}
}
}
if err := l.resolveOperationResponses(o.Responses); err != nil {
return &errpath.ErrField{Field: "responses", Err: err}
}
if err := l.resolveCallbacks(o.Callbacks); err != nil {
return &errpath.ErrField{Field: "callbacks", Err: err}
}
return nil
}
func (l *loader) resolveRequestBody(r *RequestBody) error {
if err := l.resolveContent(r.Content); err != nil {
return &errpath.ErrField{Field: "content", Err: err}
}
return nil
}
func (l *loader) resolveMediaType(mt *MediaType) error {
if mt.Schema != nil {
if err := l.resolveSchemaRef(mt.Schema); err != nil {
return &errpath.ErrField{Field: "schema", Err: err}
}
}
// if mt.Example != nil && mt.Examples != nil {
// return errors.New("example and examples are mutually exclusive")
// }
// if err := mt.Examples.Validate(); err != nil {
// return &errpath.ErrField{Field: "examples", Err: err}
// }
// if err := mt.Encoding.Validate(); err != nil {
// return &errpath.ErrField{Field: "encoding", Err: err}
// }
return nil
}
func (l *loader) resolveOperationResponses(rs OperationResponses) error {
for code, r := range rs.ByIndex() {
if err := l.resolveResponseRef(r); err != nil {
return &errpath.ErrKey{Key: string(code), Err: err}
}
}
return nil
}
func (l *loader) resolveCallbacks(cs Callbacks) error {
return nil
}
func (l *loader) collectWebhooks(ws Webhooks, ref ref) {
}