forked from mikunalpha/goas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oas.go
387 lines (311 loc) · 9.62 KB
/
oas.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
package goas
import (
"bytes"
"encoding/json"
"github.com/elliotchance/orderedmap/v2"
)
const (
OpenAPIVersion = "3.0.0"
ContentTypeText = "text/plain"
ContentTypeJson = "application/json"
ContentTypeOctetStream = "application/octet-stream"
ContentTypeForm = "multipart/form-data"
)
type OpenAPIObject struct {
OpenAPI string `json:"openapi"` // Required
Info InfoObject `json:"info"` // Required
Servers []ServerObject `json:"servers,omitempty"`
Paths PathsObject `json:"paths"` // Required
Components ComponentsOjbect `json:"components,omitempty"` // Required for Authorization header
Security []map[string][]string `json:"security,omitempty"`
// Tags
// ExternalDocs
}
type ServerObject struct {
URL string `json:"url"`
Description string `json:"description,omitempty"`
// Variables
}
type InfoObject struct {
Title string `json:"title"`
Description string `json:"description,omitempty"`
TermsOfService string `json:"termsOfService,omitempty"`
Contact *ContactObject `json:"contact,omitempty"`
License *LicenseObject `json:"license,omitempty"`
Version string `json:"version"`
}
type ContactObject struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
type LicenseObject struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
}
type PathsObject map[string]*PathItemObject
type PathItemObject struct {
Ref string `json:"$ref,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Get *OperationObject `json:"get,omitempty"`
Post *OperationObject `json:"post,omitempty"`
Patch *OperationObject `json:"patch,omitempty"`
Put *OperationObject `json:"put,omitempty"`
Delete *OperationObject `json:"delete,omitempty"`
Options *OperationObject `json:"options,omitempty"`
Head *OperationObject `json:"head,omitempty"`
Trace *OperationObject `json:"trace,omitempty"`
// Servers
// Parameters
}
type OperationObject struct {
Responses ResponsesObject `json:"responses"` // Required
Tags []string `json:"tags,omitempty"`
OperationID string `json:"operationId,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Parameters []ParameterObject `json:"parameters,omitempty"`
RequestBody *RequestBodyObject `json:"requestBody,omitempty"`
// Tags
// ExternalDocs
// OperationID
// Callbacks
// Deprecated
// Security
// Servers
}
type ParameterObject struct {
Name string `json:"name"` // Required
In string `json:"in"` // Required. Possible values are "query", "header", "path" or "cookie"
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Example interface{} `json:"example,omitempty"`
Schema *SchemaObject `json:"schema,omitempty"`
// Ref is used when ParameterOjbect is as a ReferenceObject
Ref string `json:"$ref,omitempty"`
// Deprecated
// AllowEmptyValue
// Style
// Explode
// AllowReserved
// Examples
// Content
}
type ReferenceObject struct {
Ref string `json:"$ref,omitempty"`
}
type RequestBodyObject struct {
Content map[string]*MediaTypeObject `json:"content"` // Required
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
// Ref is used when RequestBodyObject is as a ReferenceObject
Ref string `json:"$ref,omitempty"`
}
type MediaTypeObject struct {
Schema SchemaObject `json:"schema,omitempty"`
// Example string `json:"example,omitempty"`
// Examples
// Encoding
}
type Properties struct {
*orderedmap.OrderedMap[string, *SchemaObject]
}
func NewProperties() *Properties {
return &Properties{orderedmap.NewOrderedMap[string, *SchemaObject]()}
}
func (p *Properties) MarshalJSON() ([]byte, error) {
if p == nil {
return []byte("null"), nil
}
var buf bytes.Buffer
if err := buf.WriteByte('{'); err != nil {
return nil, err
}
encoder := json.NewEncoder(&buf)
first := true
for el := p.Front(); el != nil; el = el.Next() {
if !first {
if err := buf.WriteByte(','); err != nil {
return nil, err
}
}
first = false
// add key
if err := encoder.Encode(el.Key); err != nil {
return nil, err
}
if err := buf.WriteByte(':'); err != nil {
return nil, err
}
// add value
if err := encoder.Encode(el.Value); err != nil {
return nil, err
}
}
if err := buf.WriteByte('}'); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
type SchemaObject struct {
ID string `json:"-"` // For goas
PkgName string `json:"-"` // For goas
FieldName string `json:"-"` // For goas
DisabledFieldNames map[string]struct{} `json:"-"` // For goas
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
Properties *Properties `json:"properties,omitempty"`
Description string `json:"description,omitempty"`
Items *SchemaObject `json:"items,omitempty"` // use ptr to prevent recursive error
Example interface{} `json:"example,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Nullable bool `json:"nullable,omitempty"`
Minimum *int64 `json:"minimum,omitempty"`
Maximum *int64 `json:"maximum,omitempty"`
MinLength *int64 `json:"minLength,omitempty"`
MaxLength *int64 `json:"maxLength,omitempty"`
MinItems *int64 `json:"minItems,omitempty"`
MaxItems *int64 `json:"maxItems,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
// Ref is used when SchemaObject is as a ReferenceObject
Ref string `json:"$ref,omitempty"`
// Title
// MultipleOf
// Maximum
// ExclusiveMaximum
// Minimum
// ExclusiveMinimum
// MaxLength
// MinLength
// Pattern
// MaxItems
// MinItems
// UniqueItems
// MaxProperties
// MinProperties
// Enum
// AllOf
// OneOf
// AnyOf
// Not
// AdditionalProperties
// Description
// Default
// Nullable
// ReadOnly
// WriteOnly
// XML
// ExternalDocs
}
type ResponsesObject map[string]*ResponseObject // [status]ResponseObject
type Headers struct {
*orderedmap.OrderedMap[string, HeaderObject]
}
func NewHeaders() *Headers {
return &Headers{orderedmap.NewOrderedMap[string, HeaderObject]()}
}
func (h *Headers) MarshalJSON() ([]byte, error) {
if h == nil {
return []byte("null"), nil
}
var buf bytes.Buffer
if err := buf.WriteByte('{'); err != nil {
return nil, err
}
encoder := json.NewEncoder(&buf)
first := true
for el := h.Front(); el != nil; el = el.Next() {
if !first {
if err := buf.WriteByte(','); err != nil {
return nil, err
}
}
first = false
// add key
if err := encoder.Encode(el.Key); err != nil {
return nil, err
}
if err := buf.WriteByte(':'); err != nil {
return nil, err
}
// add value
if err := encoder.Encode(el.Value); err != nil {
return nil, err
}
}
if err := buf.WriteByte('}'); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
type ResponseObject struct {
Description string `json:"description"` // Required
Headers *Headers `json:"headers,omitempty"`
Content map[string]*MediaTypeObject `json:"content,omitempty"`
// Ref is for ReferenceObject
Ref string `json:"$ref,omitempty"`
// Links
}
// type HeaderObject struct {
// Description string `json:"description,omitempty"`
// Type string `json:"type,omitempty"`
// // Ref is used when HeaderObject is as a ReferenceObject
// Ref string `json:"$ref,omitempty"`
// }
type HeaderObject struct {
Schema *SchemaObject `json:"schema,omitempty"`
Description string `json:"description,omitempty"`
}
type ComponentsOjbect struct {
Schemas map[string]*SchemaObject `json:"schemas,omitempty"`
SecuritySchemes map[string]*SecuritySchemeObject `json:"securitySchemes,omitempty"`
// Responses
// Parameters
// Examples
// RequestBodies
// Headers
// Links
// Callbacks
}
type SecuritySchemeObject struct {
// Generic fields
Type string `json:"type"` // Required
Description string `json:"description,omitempty"`
// http
Scheme string `json:"scheme,omitempty"`
// apiKey
In string `json:"in,omitempty"`
Name string `json:"name,omitempty"`
// OpenID
OpenIdConnectUrl string `json:"openIdConnectUrl,omitempty"`
// OAuth2
OAuthFlows *SecuritySchemeOauthObject `json:"flows,omitempty"`
// BearerFormat
}
type SecuritySchemeOauthObject struct {
Implicit *SecuritySchemeOauthFlowObject `json:"implicit,omitempty"`
AuthorizationCode *SecuritySchemeOauthFlowObject `json:"authorizationCode,omitempty"`
ResourceOwnerPassword *SecuritySchemeOauthFlowObject `json:"password,omitempty"`
ClientCredentials *SecuritySchemeOauthFlowObject `json:"clientCredentials,omitempty"`
}
func (s *SecuritySchemeOauthObject) ApplyScopes(scopes map[string]string) {
if s.Implicit != nil {
s.Implicit.Scopes = scopes
}
if s.AuthorizationCode != nil {
s.AuthorizationCode.Scopes = scopes
}
if s.ResourceOwnerPassword != nil {
s.ResourceOwnerPassword.Scopes = scopes
}
if s.ClientCredentials != nil {
s.ClientCredentials.Scopes = scopes
}
}
type SecuritySchemeOauthFlowObject struct {
AuthorizationUrl string `json:"authorizationUrl,omitempty"`
TokenUrl string `json:"tokenUrl,omitempty"`
Scopes map[string]string `json:"scopes"`
}