-
Notifications
You must be signed in to change notification settings - Fork 72
/
resolve.go
256 lines (231 loc) · 6.41 KB
/
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
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
package parser
import (
"context"
"strings"
"github.com/go-faster/errors"
"github.com/go-faster/yaml"
"github.com/ogen-go/ogen"
"github.com/ogen-go/ogen/jsonpointer"
"github.com/ogen-go/ogen/location"
"github.com/ogen-go/ogen/openapi"
)
type resolver struct {
node *yaml.Node
file location.File
}
func (p *parser) getResolver(loc string) (r resolver, rerr error) {
r, ok := p.schemas[loc]
if ok {
return r, nil
}
raw, err := p.external.Get(context.TODO(), loc)
if err != nil {
return r, errors.Wrapf(err, "get %q", loc)
}
file := location.NewFile(loc, loc, raw)
defer func() {
if rerr != nil {
rerr = &location.Error{
File: file,
Err: rerr,
}
}
}()
var node yaml.Node
if err := yaml.Unmarshal(raw, &node); err != nil {
return r, errors.Wrap(err, "unmarshal")
}
r = resolver{
node: &node,
file: file,
}
p.schemas[loc] = r
return r, nil
}
func resolvePointer(root *yaml.Node, ptr string, to any) error {
n, err := jsonpointer.Resolve(ptr, root)
if err != nil {
return err
}
return n.Decode(to)
}
// componentResolve contains all the information needed to resolve a component.
type componentResolve[Raw, Target any] struct {
// prefix is the usual prefix of the component reference (e.g "#/components/requestBodies/").
prefix string
// components is the root spec components field.
components map[string]Raw
// cache is the cache of already resolved components.
cache map[refKey]Target
// parse is the function that parses the raw component.
parse func(Raw, *jsonpointer.ResolveCtx) (Target, error)
}
// resolveComponent is a generic function that resolves a component.
//
// We return a boolean indicating whether the component was already cached.
// Do not set the ref if it was cached.
func resolveComponent[Raw, Target any](
p *parser,
cr componentResolve[Raw, Target],
ref string,
ctx *jsonpointer.ResolveCtx,
) (key refKey, zero Target, cached bool, _ error) {
key, err := ctx.Key(ref)
if err != nil {
return key, zero, false, err
}
if r, ok := cr.cache[key]; ok {
return key, r, true, nil
}
file := p.rootFile
var raw Raw
if ctx.IsRoot(key) {
name := strings.TrimPrefix(ref, cr.prefix)
c, found := cr.components[name]
if found {
raw = c
} else {
if err := resolvePointer(p.spec.Raw, ref, &raw); err != nil {
return key, zero, false, err
}
}
} else {
r, err := p.getResolver(key.Loc)
if err != nil {
return key, zero, false, err
}
file = r.file
if err := resolvePointer(r.node, key.Ptr, &raw); err != nil {
return key, zero, false, err
}
}
if err := ctx.AddKey(key, file); err != nil {
return key, zero, false, err
}
defer func() {
ctx.Delete(key)
}()
r, err := cr.parse(raw, ctx)
if err != nil {
return key, zero, false, err
}
cr.cache[key] = r
return key, r, false, nil
}
func (p *parser) resolveCtx() *jsonpointer.ResolveCtx {
return jsonpointer.NewResolveCtx(p.rootURL, p.depthLimit)
}
func (p *parser) resolveRequestBody(ref string, ctx *jsonpointer.ResolveCtx) (*openapi.RequestBody, error) {
const prefix = "#/components/requestBodies/"
key, c, cached, err := resolveComponent(p, componentResolve[*ogen.RequestBody, *openapi.RequestBody]{
prefix: prefix,
components: p.spec.Components.RequestBodies,
cache: p.refs.requestBodies,
parse: p.parseRequestBody,
}, ref, ctx)
if err != nil {
return nil, err
}
if !cached && c.Ref.IsZero() {
c.Ref = key
}
return c, nil
}
func (p *parser) resolveResponse(ref string, ctx *jsonpointer.ResolveCtx) (*openapi.Response, error) {
const prefix = "#/components/responses/"
key, c, cached, err := resolveComponent(p, componentResolve[*ogen.Response, *openapi.Response]{
prefix: prefix,
components: p.spec.Components.Responses,
cache: p.refs.responses,
parse: p.parseResponse,
}, ref, ctx)
if err != nil {
return nil, err
}
if !cached && c.Ref.IsZero() {
c.Ref = key
}
return c, nil
}
func (p *parser) resolveParameter(ref string, ctx *jsonpointer.ResolveCtx) (*openapi.Parameter, error) {
const prefix = "#/components/parameters/"
key, c, cached, err := resolveComponent(p, componentResolve[*ogen.Parameter, *openapi.Parameter]{
prefix: prefix,
components: p.spec.Components.Parameters,
cache: p.refs.parameters,
parse: p.parseParameter,
}, ref, ctx)
if err != nil {
return nil, err
}
if !cached && c.Ref.IsZero() {
c.Ref = key
}
return c, nil
}
func (p *parser) resolveHeader(headerName, ref string, ctx *jsonpointer.ResolveCtx) (*openapi.Header, error) {
const prefix = "#/components/headers/"
key, c, cached, err := resolveComponent(p, componentResolve[*ogen.Header, *openapi.Header]{
prefix: prefix,
components: p.spec.Components.Headers,
cache: p.refs.headers,
parse: func(raw *ogen.Header, ctx *jsonpointer.ResolveCtx) (*openapi.Header, error) {
return p.parseHeader(headerName, raw, ctx)
},
}, ref, ctx)
if err != nil {
return nil, err
}
if !cached && c.Ref.IsZero() {
c.Ref = key
}
return c, nil
}
func (p *parser) resolveExample(ref string, ctx *jsonpointer.ResolveCtx) (*openapi.Example, error) {
const prefix = "#/components/examples/"
key, c, cached, err := resolveComponent(p, componentResolve[*ogen.Example, *openapi.Example]{
prefix: prefix,
components: p.spec.Components.Examples,
cache: p.refs.examples,
parse: p.parseExample,
}, ref, ctx)
if err != nil {
return nil, err
}
if !cached && c != nil && c.Ref.IsZero() {
c.Ref = key
}
return c, nil
}
func (p *parser) resolveSecurityScheme(ref string, ctx *jsonpointer.ResolveCtx) (*ogen.SecurityScheme, error) {
const prefix = "#/components/securitySchemes/"
_, c, _, err := resolveComponent(p, componentResolve[*ogen.SecurityScheme, *ogen.SecurityScheme]{
prefix: prefix,
components: p.spec.Components.SecuritySchemes,
cache: p.refs.securitySchemes,
parse: p.parseSecurityScheme,
}, ref, ctx)
if err != nil {
return nil, err
}
return c, nil
}
func (p *parser) resolvePathItem(
itemPath unparsedPath,
ref string,
ctx *jsonpointer.ResolveCtx,
) (pathItem, error) {
const prefix = "#/components/pathItems/"
_, c, _, err := resolveComponent(p, componentResolve[*ogen.PathItem, pathItem]{
prefix: prefix,
components: p.spec.Components.PathItems,
cache: p.refs.pathItems,
parse: func(raw *ogen.PathItem, ctx *jsonpointer.ResolveCtx) (pathItem, error) {
return p.parsePathItem(itemPath, raw, ctx)
},
}, ref, ctx)
if err != nil {
return nil, err
}
return c, nil
}