-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3_model.go
258 lines (219 loc) · 6.09 KB
/
v3_model.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
package lib
import (
"fmt"
"github.com/cubahno/connexions/config"
"github.com/cubahno/connexions/openapi"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3high "github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"sort"
"strings"
"sync"
)
// V3Document is a wrapper around libopenapi.DocumentModel
// Implements Document interface
type V3Document struct {
*libopenapi.DocumentModel[v3high.Document]
}
// V3Operation is a wrapper around libopenapi.Operation
type V3Operation struct {
*v3high.Operation
parseConfig *config.ParseConfig
mu sync.Mutex
}
// Provider returns the SchemaProvider for this document
func (d *V3Document) Provider() config.SchemaProvider {
return config.LibOpenAPIProvider
}
// GetVersion returns the version of the document
func (d *V3Document) GetVersion() string {
return d.Model.Version
}
// GetResources returns a map of resource names and their methods.
func (d *V3Document) GetResources() map[string][]string {
res := make(map[string][]string)
if d.DocumentModel == nil || d.Model.Paths == nil {
return res
}
for name, path := range d.Model.Paths.PathItems {
res[name] = make([]string, 0)
for method := range path.GetOperations() {
res[name] = append(res[name], strings.ToUpper(method))
}
}
return res
}
// FindOperation finds an operation by resource and method.
func (d *V3Document) FindOperation(options *openapi.OperationDescription) openapi.Operation {
if options == nil {
return nil
}
path, ok := d.Model.Paths.PathItems[options.Resource]
if !ok {
return nil
}
for m, op := range path.GetOperations() {
if strings.EqualFold(m, options.Method) {
return &V3Operation{
Operation: op,
}
}
}
return nil
}
// ID returns the operation ID
func (op *V3Operation) ID() string {
return op.Operation.OperationId
}
// GetParameters returns a list of parameters for the operation
func (op *V3Operation) GetParameters() openapi.Parameters {
params := make(openapi.Parameters, 0)
for _, param := range op.Parameters {
var schema *openapi.Schema
if param.Schema != nil {
px := param.Schema
schema = NewSchema(px.Schema(), op.parseConfig)
}
params = append(params, &openapi.Parameter{
Name: param.Name,
In: param.In,
Required: param.Required,
Schema: schema,
Example: param.Example,
})
}
// original names not sorted
sort.Slice(params, func(i, j int) bool {
return params[i].Name < params[j].Name
})
return params
}
// GetResponse returns the response for the operation.
// If no response is defined, a default response is returned.
// Responses are prioritized by status code, with 200 being the highest priority.
func (op *V3Operation) GetResponse() *openapi.Response {
if op.Responses == nil {
return &openapi.Response{
StatusCode: http.StatusOK,
}
}
available := op.Responses.Codes
var responseRef *v3high.Response
statusCode := http.StatusOK
for _, code := range []int{http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent} {
responseRef = available[fmt.Sprintf("%v", code)]
if responseRef != nil {
statusCode = code
break
}
}
// Get first defined
if responseRef == nil {
for codeName, respRef := range available {
// There's no default expected in this library implementation
responseRef = respRef
statusCode = openapi.TransformHTTPCode(codeName)
break
}
}
if responseRef == nil {
responseRef = op.Responses.Default
}
if responseRef == nil {
return &openapi.Response{
StatusCode: statusCode,
}
}
parsedHeaders := make(openapi.Headers)
for name, header := range responseRef.Headers {
var schema *openapi.Schema
if header.Schema != nil {
hSchema := header.Schema.Schema()
schema = NewSchema(hSchema, op.parseConfig)
}
name = strings.ToLower(name)
parsedHeaders[name] = &openapi.Parameter{
Name: name,
In: openapi.ParameterInHeader,
Required: header.Required,
Schema: schema,
}
}
if len(parsedHeaders) == 0 {
parsedHeaders = nil
}
libContent, contentType := op.getContent(responseRef.Content)
content := NewSchema(libContent, op.parseConfig)
return &openapi.Response{
Headers: parsedHeaders,
Content: content,
ContentType: contentType,
StatusCode: statusCode,
}
}
func (op *V3Operation) getContent(contentTypes map[string]*v3high.MediaType) (*base.Schema, string) {
if len(contentTypes) == 0 {
contentTypes = make(map[string]*v3high.MediaType)
}
prioTypes := []string{"application/json", "text/plain", "text/html"}
for _, contentType := range prioTypes {
if _, ok := contentTypes[contentType]; ok {
schemaRef := contentTypes[contentType].Schema
if schemaRef == nil {
continue
}
return contentTypes[contentType].Schema.Schema(), contentType
}
}
// If none of the priority types are found, return the first available media type
for contentType, mediaType := range contentTypes {
schemaRef := mediaType.Schema
if schemaRef == nil {
continue
}
return schemaRef.Schema(), contentType
}
return nil, ""
}
// GetRequestBody returns the request body for the operation.
func (op *V3Operation) GetRequestBody() (*openapi.Schema, string) {
if op.RequestBody == nil {
return nil, ""
}
contentTypes := op.RequestBody.Content
if len(contentTypes) == 0 {
contentTypes = make(map[string]*v3high.MediaType)
}
typesOrder := []string{
"application/json",
"multipart/form-data",
"application/x-www-form-urlencoded",
"application/octet-stream",
}
for _, contentType := range typesOrder {
if _, ok := contentTypes[contentType]; ok {
px := contentTypes[contentType].Schema
if px == nil {
continue
}
return NewSchema(px.Schema(), op.parseConfig), contentType
}
}
// Get first defined
for contentType, mediaType := range contentTypes {
px := mediaType.Schema
if px == nil {
continue
}
return NewSchema(px.Schema(), op.parseConfig), contentType
}
return nil, ""
}
// WithParseConfig sets the ParseConfig for the operation.
func (op *V3Operation) WithParseConfig(parseConfig *config.ParseConfig) openapi.Operation {
op.mu.Lock()
defer op.mu.Unlock()
op.parseConfig = parseConfig
return op
}