forked from mikunalpha/goas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oas.go
265 lines (213 loc) · 7.16 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
package main
import "github.com/iancoleman/orderedmap"
const (
OpenAPIVersion = "3.0.0"
ContentTypeText = "text/plain"
ContentTypeJson = "application/json"
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"`
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 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 *orderedmap.OrderedMap `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"`
// 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 ResponseObject struct {
Description string `json:"description"` // Required
Headers map[string]*HeaderObject `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 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"`
}